LLVM 24.0.0git
SparcInstrInfo.cpp
Go to the documentation of this file.
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"
16#include "SparcSubtarget.h"
23
24using namespace llvm;
25
26#define GET_INSTRINFO_CTOR_DTOR
27#include "SparcGenInstrInfo.inc"
28
30 "sparc-bpcc-offset-bits", cl::Hidden, cl::init(19),
31 cl::desc("Restrict range of BPcc/FBPfcc instructions (DEBUG)"));
32
34 BPrDisplacementBits("sparc-bpr-offset-bits", cl::Hidden, cl::init(16),
35 cl::desc("Restrict range of BPr instructions (DEBUG)"));
36
37// Pin the vtable to this file.
38void SparcInstrInfo::anchor() {}
39
41 : SparcGenInstrInfo(ST, RI, SP::ADJCALLSTACKDOWN, SP::ADJCALLSTACKUP),
42 RI(ST), Subtarget(ST) {}
43
46 return Subtarget.is64Bit() ? &SP::I64RegsRegClass : &SP::IntRegsRegClass;
47}
48
49/// isLoadFromStackSlot - If the specified machine instruction is a direct
50/// load from a stack slot, return the virtual or physical register number of
51/// the destination along with the FrameIndex of the loaded stack slot. If
52/// not, return 0. This predicate must return 0 if the instruction has
53/// any side effects other than loading from the stack slot.
55 int &FrameIndex,
56 TypeSize &MemBytes) const {
57 switch (MI.getOpcode()) {
58 default:
59 return 0;
60 case SP::LDri:
61 MemBytes = TypeSize::getFixed(4);
62 break;
63 case SP::LDXri:
64 MemBytes = TypeSize::getFixed(8);
65 break;
66 case SP::LDFri:
67 MemBytes = TypeSize::getFixed(4);
68 break;
69 case SP::LDDFri:
70 MemBytes = TypeSize::getFixed(8);
71 break;
72 case SP::LDQFri:
73 MemBytes = TypeSize::getFixed(16);
74 break;
75 }
76 if (MI.getOperand(1).isFI() && MI.getOperand(2).isImm() &&
77 MI.getOperand(2).getImm() == 0) {
78 FrameIndex = MI.getOperand(1).getIndex();
79 return MI.getOperand(0).getReg();
80 }
81 return 0;
82}
83
84/// isStoreToStackSlot - If the specified machine instruction is a direct
85/// store to a stack slot, return the virtual or physical register number of
86/// the source reg along with the FrameIndex of the loaded stack slot. If
87/// not, return 0. This predicate must return 0 if the instruction has
88/// any side effects other than storing to the stack slot.
90 int &FrameIndex,
91 TypeSize &MemBytes) const {
92 switch (MI.getOpcode()) {
93 default:
94 return 0;
95 case SP::STri:
96 MemBytes = TypeSize::getFixed(4);
97 break;
98 case SP::STXri:
99 MemBytes = TypeSize::getFixed(8);
100 break;
101 case SP::STFri:
102 MemBytes = TypeSize::getFixed(4);
103 break;
104 case SP::STDFri:
105 MemBytes = TypeSize::getFixed(8);
106 break;
107 case SP::STQFri:
108 MemBytes = TypeSize::getFixed(16);
109 break;
110 }
111 if (MI.getOperand(0).isFI() && MI.getOperand(1).isImm() &&
112 MI.getOperand(1).getImm() == 0) {
113 FrameIndex = MI.getOperand(0).getIndex();
114 return MI.getOperand(2).getReg();
115 }
116 return 0;
117}
118
120{
121 switch(CC) {
122 case SPCC::ICC_A: return SPCC::ICC_N;
123 case SPCC::ICC_N: return SPCC::ICC_A;
124 case SPCC::ICC_NE: return SPCC::ICC_E;
125 case SPCC::ICC_E: return SPCC::ICC_NE;
126 case SPCC::ICC_G: return SPCC::ICC_LE;
127 case SPCC::ICC_LE: return SPCC::ICC_G;
128 case SPCC::ICC_GE: return SPCC::ICC_L;
129 case SPCC::ICC_L: return SPCC::ICC_GE;
130 case SPCC::ICC_GU: return SPCC::ICC_LEU;
131 case SPCC::ICC_LEU: return SPCC::ICC_GU;
132 case SPCC::ICC_CC: return SPCC::ICC_CS;
133 case SPCC::ICC_CS: return SPCC::ICC_CC;
134 case SPCC::ICC_POS: return SPCC::ICC_NEG;
135 case SPCC::ICC_NEG: return SPCC::ICC_POS;
136 case SPCC::ICC_VC: return SPCC::ICC_VS;
137 case SPCC::ICC_VS: return SPCC::ICC_VC;
138
139 case SPCC::FCC_A: return SPCC::FCC_N;
140 case SPCC::FCC_N: return SPCC::FCC_A;
141 case SPCC::FCC_U: return SPCC::FCC_O;
142 case SPCC::FCC_O: return SPCC::FCC_U;
143 case SPCC::FCC_G: return SPCC::FCC_ULE;
144 case SPCC::FCC_LE: return SPCC::FCC_UG;
145 case SPCC::FCC_UG: return SPCC::FCC_LE;
146 case SPCC::FCC_ULE: return SPCC::FCC_G;
147 case SPCC::FCC_L: return SPCC::FCC_UGE;
148 case SPCC::FCC_GE: return SPCC::FCC_UL;
149 case SPCC::FCC_UL: return SPCC::FCC_GE;
150 case SPCC::FCC_UGE: return SPCC::FCC_L;
151 case SPCC::FCC_LG: return SPCC::FCC_UE;
152 case SPCC::FCC_UE: return SPCC::FCC_LG;
153 case SPCC::FCC_NE: return SPCC::FCC_E;
154 case SPCC::FCC_E: return SPCC::FCC_NE;
155
156 case SPCC::CPCC_A: return SPCC::CPCC_N;
157 case SPCC::CPCC_N: return SPCC::CPCC_A;
158 case SPCC::CPCC_3: [[fallthrough]];
159 case SPCC::CPCC_2: [[fallthrough]];
160 case SPCC::CPCC_23: [[fallthrough]];
161 case SPCC::CPCC_1: [[fallthrough]];
162 case SPCC::CPCC_13: [[fallthrough]];
163 case SPCC::CPCC_12: [[fallthrough]];
164 case SPCC::CPCC_123: [[fallthrough]];
165 case SPCC::CPCC_0: [[fallthrough]];
166 case SPCC::CPCC_03: [[fallthrough]];
167 case SPCC::CPCC_02: [[fallthrough]];
168 case SPCC::CPCC_023: [[fallthrough]];
169 case SPCC::CPCC_01: [[fallthrough]];
170 case SPCC::CPCC_013: [[fallthrough]];
171 case SPCC::CPCC_012:
172 // "Opposite" code is not meaningful, as we don't know
173 // what the CoProc condition means here. The cond-code will
174 // only be used in inline assembler, so this code should
175 // not be reached in a normal compilation pass.
176 llvm_unreachable("Meaningless inversion of co-processor cond code");
177
178 case SPCC::REG_BEGIN:
179 llvm_unreachable("Use of reserved cond code");
180 case SPCC::REG_Z:
181 return SPCC::REG_NZ;
182 case SPCC::REG_LEZ:
183 return SPCC::REG_GZ;
184 case SPCC::REG_LZ:
185 return SPCC::REG_GEZ;
186 case SPCC::REG_NZ:
187 return SPCC::REG_Z;
188 case SPCC::REG_GZ:
189 return SPCC::REG_LEZ;
190 case SPCC::REG_GEZ:
191 return SPCC::REG_LZ;
192 }
193 llvm_unreachable("Invalid cond code");
194}
195
196static bool isUncondBranchOpcode(int Opc) { return Opc == SP::BA; }
197
198static bool isI32CondBranchOpcode(int Opc) {
199 return Opc == SP::BCOND || Opc == SP::BPICC || Opc == SP::BPICCA ||
200 Opc == SP::BPICCNT || Opc == SP::BPICCANT;
201}
202
203static bool isI64CondBranchOpcode(int Opc) {
204 return Opc == SP::BPXCC || Opc == SP::BPXCCA || Opc == SP::BPXCCNT ||
205 Opc == SP::BPXCCANT;
206}
207
208static bool isRegCondBranchOpcode(int Opc) {
209 return Opc == SP::BPR || Opc == SP::BPRA || Opc == SP::BPRNT ||
210 Opc == SP::BPRANT;
211}
212
213static bool isFCondBranchOpcode(int Opc) {
214 return Opc == SP::FBCOND || Opc == SP::FBCONDA || Opc == SP::FBCOND_V9 ||
215 Opc == SP::FBCONDA_V9;
216}
217
222
223static bool isIndirectBranchOpcode(int Opc) {
224 return Opc == SP::BINDrr || Opc == SP::BINDri;
225}
226
229 unsigned Opc = LastInst->getOpcode();
230 int64_t CC = LastInst->getOperand(1).getImm();
231
232 // Push the branch opcode into Cond too so later in insertBranch
233 // it can use the information to emit the correct SPARC branch opcode.
235 Cond.push_back(MachineOperand::CreateImm(CC));
236
237 // Branch on register contents need another argument to indicate
238 // the register it branches on.
240 Register Reg = LastInst->getOperand(2).getReg();
241 Cond.push_back(MachineOperand::CreateReg(Reg, false));
242 }
243
244 Target = LastInst->getOperand(0).getMBB();
245}
246
249 switch (MI.getOpcode()) {
250 default:
251 llvm_unreachable("unexpected opcode!");
252 case SP::BA:
253 case SP::BCOND:
254 case SP::BCONDA:
255 case SP::FBCOND:
256 case SP::FBCONDA:
257 case SP::BPICC:
258 case SP::BPICCA:
259 case SP::BPICCNT:
260 case SP::BPICCANT:
261 case SP::BPXCC:
262 case SP::BPXCCA:
263 case SP::BPXCCNT:
264 case SP::BPXCCANT:
265 case SP::BPFCC:
266 case SP::BPFCCA:
267 case SP::BPFCCNT:
268 case SP::BPFCCANT:
269 case SP::FBCOND_V9:
270 case SP::FBCONDA_V9:
271 case SP::BPR:
272 case SP::BPRA:
273 case SP::BPRNT:
274 case SP::BPRANT:
275 return MI.getOperand(0).getMBB();
276 }
277}
278
281 MachineBasicBlock *&FBB,
283 bool AllowModify) const {
284 MachineBasicBlock::iterator I = MBB.getLastNonDebugInstr();
285 if (I == MBB.end())
286 return false;
287
288 if (!isUnpredicatedTerminator(*I))
289 return false;
290
291 // Get the last instruction in the block.
292 MachineInstr *LastInst = &*I;
293 unsigned LastOpc = LastInst->getOpcode();
294
295 // If there is only one terminator instruction, process it.
296 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
297 if (isUncondBranchOpcode(LastOpc)) {
298 TBB = LastInst->getOperand(0).getMBB();
299 return false;
300 }
301 if (isCondBranchOpcode(LastOpc)) {
302 // Block ends with fall-through condbranch.
303 parseCondBranch(LastInst, TBB, Cond);
304 return false;
305 }
306 return true; // Can't handle indirect branch.
307 }
308
309 // Get the instruction before it if it is a terminator.
310 MachineInstr *SecondLastInst = &*I;
311 unsigned SecondLastOpc = SecondLastInst->getOpcode();
312
313 // If AllowModify is true and the block ends with two or more unconditional
314 // branches, delete all but the first unconditional branch.
315 if (AllowModify && isUncondBranchOpcode(LastOpc)) {
316 while (isUncondBranchOpcode(SecondLastOpc)) {
317 LastInst->eraseFromParent();
318 LastInst = SecondLastInst;
319 LastOpc = LastInst->getOpcode();
320 if (I == MBB.begin() || !isUnpredicatedTerminator(*--I)) {
321 // Return now the only terminator is an unconditional branch.
322 TBB = LastInst->getOperand(0).getMBB();
323 return false;
324 } else {
325 SecondLastInst = &*I;
326 SecondLastOpc = SecondLastInst->getOpcode();
327 }
328 }
329 }
330
331 // If there are three terminators, we don't know what sort of block this is.
332 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(*--I))
333 return true;
334
335 // If the block ends with a B and a Bcc, handle it.
336 if (isCondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
337 parseCondBranch(SecondLastInst, TBB, Cond);
338 FBB = LastInst->getOperand(0).getMBB();
339 return false;
340 }
341
342 // If the block ends with two unconditional branches, handle it. The second
343 // one is not executed.
344 if (isUncondBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
345 TBB = SecondLastInst->getOperand(0).getMBB();
346 return false;
347 }
348
349 // ...likewise if it ends with an indirect branch followed by an unconditional
350 // branch.
351 if (isIndirectBranchOpcode(SecondLastOpc) && isUncondBranchOpcode(LastOpc)) {
352 I = LastInst;
353 if (AllowModify)
354 I->eraseFromParent();
355 return true;
356 }
357
358 // Otherwise, can't handle this.
359 return true;
360}
361
366 const DebugLoc &DL,
367 int *BytesAdded) const {
368 assert(TBB && "insertBranch must not be told to insert a fallthrough");
369 assert((Cond.size() <= 3) &&
370 "Sparc branch conditions should have at most three components!");
371
372 if (Cond.empty()) {
373 assert(!FBB && "Unconditional branch with multiple successors!");
374 BuildMI(&MBB, DL, get(SP::BA)).addMBB(TBB);
375 if (BytesAdded)
376 *BytesAdded = 8;
377 return 1;
378 }
379
380 // Conditional branch
381 unsigned Opc = Cond[0].getImm();
382 unsigned CC = Cond[1].getImm();
384 Register Reg = Cond[2].getReg();
385 BuildMI(&MBB, DL, get(Opc)).addMBB(TBB).addImm(CC).addReg(Reg);
386 } else {
387 BuildMI(&MBB, DL, get(Opc)).addMBB(TBB).addImm(CC);
388 }
389
390 if (!FBB) {
391 if (BytesAdded)
392 *BytesAdded = 8;
393 return 1;
394 }
395
396 BuildMI(&MBB, DL, get(SP::BA)).addMBB(FBB);
397 if (BytesAdded)
398 *BytesAdded = 16;
399 return 2;
400}
401
403 int *BytesRemoved) const {
405 unsigned Count = 0;
406 int Removed = 0;
407 while (I != MBB.begin()) {
408 --I;
409
410 if (I->isDebugInstr())
411 continue;
412
413 if (!isCondBranchOpcode(I->getOpcode()) &&
414 !isUncondBranchOpcode(I->getOpcode()))
415 break; // Not a branch
416
417 Removed += getInstSizeInBytes(*I);
418 I->eraseFromParent();
419 I = MBB.end();
420 ++Count;
421 }
422
423 if (BytesRemoved)
424 *BytesRemoved = Removed;
425 return Count;
426}
427
430 assert(Cond.size() <= 3);
431 SPCC::CondCodes CC = static_cast<SPCC::CondCodes>(Cond[1].getImm());
432 Cond[1].setImm(GetOppositeBranchCondition(CC));
433 return false;
434}
435
437 int64_t Offset) const {
438 assert((Offset & 0b11) == 0 && "Malformed branch offset");
439 switch (BranchOpc) {
440 case SP::BA:
441 case SP::BCOND:
442 case SP::BCONDA:
443 case SP::FBCOND:
444 case SP::FBCONDA:
445 return isIntN(22, Offset >> 2);
446
447 case SP::BPICC:
448 case SP::BPICCA:
449 case SP::BPICCNT:
450 case SP::BPICCANT:
451 case SP::BPXCC:
452 case SP::BPXCCA:
453 case SP::BPXCCNT:
454 case SP::BPXCCANT:
455 case SP::BPFCC:
456 case SP::BPFCCA:
457 case SP::BPFCCNT:
458 case SP::BPFCCANT:
459 case SP::FBCOND_V9:
460 case SP::FBCONDA_V9:
461 return isIntN(BPccDisplacementBits, Offset >> 2);
462
463 case SP::BPR:
464 case SP::BPRA:
465 case SP::BPRNT:
466 case SP::BPRANT:
467 return isIntN(BPrDisplacementBits, Offset >> 2);
468 }
469
470 llvm_unreachable("Unknown branch instruction!");
471}
472
475 const DebugLoc &DL, Register DestReg,
476 Register SrcReg, bool KillSrc,
477 bool RenamableDest, bool RenamableSrc) const {
478 unsigned numSubRegs = 0;
479 unsigned movOpc = 0;
480 const unsigned *subRegIdx = nullptr;
481 bool ExtraG0 = false;
482
483 const unsigned DW_SubRegsIdx[] = { SP::sub_even, SP::sub_odd };
484 const unsigned DFP_FP_SubRegsIdx[] = { SP::sub_even, SP::sub_odd };
485 const unsigned QFP_DFP_SubRegsIdx[] = { SP::sub_even64, SP::sub_odd64 };
486 const unsigned QFP_FP_SubRegsIdx[] = { SP::sub_even, SP::sub_odd,
487 SP::sub_odd64_then_sub_even,
488 SP::sub_odd64_then_sub_odd };
489
490 if (SP::IntRegsRegClass.contains(DestReg, SrcReg))
491 BuildMI(MBB, I, DL, get(SP::ORrr), DestReg).addReg(SP::G0)
492 .addReg(SrcReg, getKillRegState(KillSrc));
493 else if (SP::IntPairRegClass.contains(DestReg, SrcReg)) {
494 subRegIdx = DW_SubRegsIdx;
495 numSubRegs = 2;
496 movOpc = SP::ORrr;
497 ExtraG0 = true;
498 } else if (SP::FPRegsRegClass.contains(DestReg, SrcReg))
499 BuildMI(MBB, I, DL, get(SP::FMOVS), DestReg)
500 .addReg(SrcReg, getKillRegState(KillSrc));
501 else if (SP::DFPRegsRegClass.contains(DestReg, SrcReg)) {
502 if (Subtarget.isV9()) {
503 BuildMI(MBB, I, DL, get(SP::FMOVD), DestReg)
504 .addReg(SrcReg, getKillRegState(KillSrc));
505 } else {
506 // Use two FMOVS instructions.
507 subRegIdx = DFP_FP_SubRegsIdx;
508 numSubRegs = 2;
509 movOpc = SP::FMOVS;
510 }
511 } else if (SP::QFPRegsRegClass.contains(DestReg, SrcReg)) {
512 if (Subtarget.isV9()) {
513 if (Subtarget.hasHardQuad()) {
514 BuildMI(MBB, I, DL, get(SP::FMOVQ), DestReg)
515 .addReg(SrcReg, getKillRegState(KillSrc));
516 } else {
517 // Use two FMOVD instructions.
518 subRegIdx = QFP_DFP_SubRegsIdx;
519 numSubRegs = 2;
520 movOpc = SP::FMOVD;
521 }
522 } else {
523 // Use four FMOVS instructions.
524 subRegIdx = QFP_FP_SubRegsIdx;
525 numSubRegs = 4;
526 movOpc = SP::FMOVS;
527 }
528 } else if (SP::ASRRegsRegClass.contains(DestReg) &&
529 SP::IntRegsRegClass.contains(SrcReg)) {
530 BuildMI(MBB, I, DL, get(SP::WRASRrr), DestReg)
531 .addReg(SP::G0)
532 .addReg(SrcReg, getKillRegState(KillSrc));
533 } else if (SP::IntRegsRegClass.contains(DestReg) &&
534 SP::ASRRegsRegClass.contains(SrcReg)) {
535 BuildMI(MBB, I, DL, get(SP::RDASR), DestReg)
536 .addReg(SrcReg, getKillRegState(KillSrc));
537 } else
538 llvm_unreachable("Impossible reg-to-reg copy");
539
540 if (numSubRegs == 0 || subRegIdx == nullptr || movOpc == 0)
541 return;
542
544 MachineInstr *MovMI = nullptr;
545
546 for (unsigned i = 0; i != numSubRegs; ++i) {
547 Register Dst = TRI->getSubReg(DestReg, subRegIdx[i]);
548 Register Src = TRI->getSubReg(SrcReg, subRegIdx[i]);
549 assert(Dst && Src && "Bad sub-register");
550
551 MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(movOpc), Dst);
552 if (ExtraG0)
553 MIB.addReg(SP::G0);
554 MIB.addReg(Src);
555 MovMI = MIB.getInstr();
556 }
557 // Add implicit super-register defs and kills to the last MovMI.
558 MovMI->addRegisterDefined(DestReg, TRI);
559 if (KillSrc)
560 MovMI->addRegisterKilled(SrcReg, TRI);
561}
562
565 Register SrcReg, bool isKill, int FI,
566 const TargetRegisterClass *RC,
567 Register VReg,
568 MachineInstr::MIFlag Flags) const {
569 DebugLoc DL;
570 if (I != MBB.end()) DL = I->getDebugLoc();
571
572 MachineFunction *MF = MBB.getParent();
573 const MachineFrameInfo &MFI = MF->getFrameInfo();
576 MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
577
578 // On the order of operands here: think "[FrameIdx + 0] = SrcReg".
579 if (RC == &SP::I64RegsRegClass)
580 BuildMI(MBB, I, DL, get(SP::STXri)).addFrameIndex(FI).addImm(0)
581 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
582 else if (RC == &SP::IntRegsRegClass)
583 BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0)
584 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
585 else if (RC == &SP::IntPairRegClass)
586 BuildMI(MBB, I, DL, get(SP::STDri)).addFrameIndex(FI).addImm(0)
587 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
588 else if (RC == &SP::FPRegsRegClass)
589 BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0)
590 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
591 else if (SP::DFPRegsRegClass.hasSubClassEq(RC))
592 BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0)
593 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
594 else if (SP::QFPRegsRegClass.hasSubClassEq(RC))
595 // Use STQFri irrespective of its legality. If STQ is not legal, it will be
596 // lowered into two STDs in eliminateFrameIndex.
597 BuildMI(MBB, I, DL, get(SP::STQFri)).addFrameIndex(FI).addImm(0)
598 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO);
599 else
600 llvm_unreachable("Can't store this register to stack slot");
601}
602
605 Register DestReg, int FI,
606 const TargetRegisterClass *RC,
607 Register VReg, unsigned SubReg,
608 MachineInstr::MIFlag Flags) const {
609 DebugLoc DL;
610 if (I != MBB.end()) DL = I->getDebugLoc();
611
612 MachineFunction *MF = MBB.getParent();
613 const MachineFrameInfo &MFI = MF->getFrameInfo();
616 MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
617
618 if (RC == &SP::I64RegsRegClass)
619 BuildMI(MBB, I, DL, get(SP::LDXri), DestReg).addFrameIndex(FI).addImm(0)
620 .addMemOperand(MMO);
621 else if (RC == &SP::IntRegsRegClass)
622 BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0)
623 .addMemOperand(MMO);
624 else if (RC == &SP::IntPairRegClass)
625 BuildMI(MBB, I, DL, get(SP::LDDri), DestReg).addFrameIndex(FI).addImm(0)
626 .addMemOperand(MMO);
627 else if (RC == &SP::FPRegsRegClass)
628 BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0)
629 .addMemOperand(MMO);
630 else if (SP::DFPRegsRegClass.hasSubClassEq(RC))
631 BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0)
632 .addMemOperand(MMO);
633 else if (SP::QFPRegsRegClass.hasSubClassEq(RC))
634 // Use LDQFri irrespective of its legality. If LDQ is not legal, it will be
635 // lowered into two LDDs in eliminateFrameIndex.
636 BuildMI(MBB, I, DL, get(SP::LDQFri), DestReg).addFrameIndex(FI).addImm(0)
637 .addMemOperand(MMO);
638 else
639 llvm_unreachable("Can't load this register from stack slot");
640}
641
644 Register GlobalBaseReg = SparcFI->getGlobalBaseReg();
645 if (GlobalBaseReg)
646 return GlobalBaseReg;
647
648 // Insert the set of GlobalBaseReg into the first MBB of the function
649 MachineBasicBlock &FirstMBB = MF->front();
651 MachineRegisterInfo &RegInfo = MF->getRegInfo();
652
653 const TargetRegisterClass *PtrRC =
654 Subtarget.is64Bit() ? &SP::I64RegsRegClass : &SP::IntRegsRegClass;
655 GlobalBaseReg = RegInfo.createVirtualRegister(PtrRC);
656
657 DebugLoc dl;
658
659 BuildMI(FirstMBB, MBBI, dl, get(SP::GETPCX), GlobalBaseReg);
660 SparcFI->setGlobalBaseReg(GlobalBaseReg);
661 return GlobalBaseReg;
662}
663
665 unsigned &StructSize) const {
666 if (!MI.isCall())
667 return false;
668
669 unsigned StructSizeOpNum = 0;
670 switch (MI.getOpcode()) {
671 default:
672 llvm_unreachable("Unknown call opcode.");
673 case SP::CALL:
674 StructSizeOpNum = 1;
675 break;
676 case SP::CALLrr:
677 case SP::CALLri:
678 StructSizeOpNum = 2;
679 break;
680 case SP::TLS_CALL:
681 return false;
682 case SP::TAIL_CALLri:
683 case SP::TAIL_CALL:
684 return false;
685 }
686
687 const MachineOperand &MO = MI.getOperand(StructSizeOpNum);
688 if (!MO.isImm())
689 return false;
690
691 // A zero-sized return value has nothing for the callee to copy, so GCC emits
692 // no unimp for it and returns to the instruction right after the delay slot.
693 // We replicate this behavior here.
694 StructSize = MO.getImm();
695 return StructSize != 0;
696}
697
699 unsigned Opcode = MI.getOpcode();
700
701 if (MI.isInlineAsm()) {
702 const MachineFunction *MF = MI.getParent()->getParent();
703 const char *AsmStr = MI.getOperand(0).getSymbolName();
704 return getInlineAsmLength(AsmStr, MF->getTarget().getMCAsmInfo());
705 }
706
707 if (Opcode == TargetOpcode::BUNDLE)
708 return getInstBundleSize(MI);
709
710 if (MI.getOpcode() == SP::GETPCX) {
711 const TargetMachine &TM = MI.getParent()->getParent()->getTarget();
712 if (TM.isPositionIndependent())
713 return 16;
714 switch (TM.getCodeModel()) {
715 default:
716 llvm_unreachable("Unsupported absolute code model");
717 case CodeModel::Small:
718 return 8;
720 return 16;
721 case CodeModel::Large:
722 return 24;
723 }
724 }
725
726 // If the instruction has a delay slot, be conservative and also include
727 // it for sizing purposes. This is done so that the BranchRelaxation pass
728 // will not mistakenly mark out-of-range branches as in-range.
729 if (MI.hasDelaySlot()) {
730 unsigned StructSize = 0;
731 return get(Opcode).getSize() * (2 + needsUnimp(MI, StructSize));
732 }
733 return get(Opcode).getSize();
734}
735
737 Register &SrcReg2, int64_t &CmpMask,
738 int64_t &CmpValue) const {
739 Register DstReg;
740 switch (MI.getOpcode()) {
741 default:
742 break;
743 case SP::SUBCCri:
744 DstReg = MI.getOperand(0).getReg();
745 SrcReg = MI.getOperand(1).getReg();
746 SrcReg2 = 0;
747 CmpMask = ~0;
748 CmpValue = MI.getOperand(2).getImm();
749 return DstReg == SP::G0 && CmpValue == 0;
750 case SP::SUBCCrr:
751 DstReg = MI.getOperand(0).getReg();
752 SrcReg = MI.getOperand(1).getReg();
753 SrcReg2 = MI.getOperand(2).getReg();
754 CmpMask = ~0;
755 CmpValue = 0;
756 return DstReg == SP::G0 && SrcReg2 == SP::G0;
757 }
758
759 return false;
760}
761
763 MachineInstr &CmpInstr, Register SrcReg, Register SrcReg2, int64_t CmpMask,
764 int64_t CmpValue, const MachineRegisterInfo *MRI) const {
765
766 // Get the unique definition of SrcReg.
767 MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
768 if (!MI)
769 return false;
770
771 // Only optimize if defining and comparing instruction in same block.
772 if (MI->getParent() != CmpInstr.getParent())
773 return false;
774
775 unsigned NewOpcode;
776 switch (MI->getOpcode()) {
777 case SP::ANDNrr:
778 NewOpcode = SP::ANDNCCrr;
779 break;
780 case SP::ANDNri:
781 NewOpcode = SP::ANDNCCri;
782 break;
783 case SP::ANDrr:
784 NewOpcode = SP::ANDCCrr;
785 break;
786 case SP::ANDri:
787 NewOpcode = SP::ANDCCri;
788 break;
789 case SP::ORrr:
790 NewOpcode = SP::ORCCrr;
791 break;
792 case SP::ORri:
793 NewOpcode = SP::ORCCri;
794 break;
795 case SP::ORNCCrr:
796 NewOpcode = SP::ORNCCrr;
797 break;
798 case SP::ORNri:
799 NewOpcode = SP::ORNCCri;
800 break;
801 case SP::XORrr:
802 NewOpcode = SP::XORCCrr;
803 break;
804 case SP::XNORri:
805 NewOpcode = SP::XNORCCri;
806 break;
807 case SP::XNORrr:
808 NewOpcode = SP::XNORCCrr;
809 break;
810 case SP::ADDrr:
811 NewOpcode = SP::ADDCCrr;
812 break;
813 case SP::ADDri:
814 NewOpcode = SP::ADDCCri;
815 break;
816 case SP::SUBrr:
817 NewOpcode = SP::SUBCCrr;
818 break;
819 case SP::SUBri:
820 NewOpcode = SP::SUBCCri;
821 break;
822 default:
823 return false;
824 }
825
826 bool IsICCModified = false;
829 MachineBasicBlock::iterator E = CmpInstr.getParent()->end();
831
832 // If ICC is used or modified between MI and CmpInstr we cannot optimize.
833 while (++I != C) {
834 if (I->modifiesRegister(SP::ICC, TRI) || I->readsRegister(SP::ICC, TRI))
835 return false;
836 }
837
838 while (++I != E) {
839 // Only allow conditionals on equality.
840 if (I->readsRegister(SP::ICC, TRI)) {
841 bool IsICCBranch = I->getOpcode() == SP::BCOND ||
842 I->getOpcode() == SP::BPICC ||
843 I->getOpcode() == SP::BPXCC;
844 bool IsICCMove =
845 I->getOpcode() == SP::MOVICCrr || I->getOpcode() == SP::MOVICCri ||
846 I->getOpcode() == SP::MOVXCCrr || I->getOpcode() == SP::MOVXCCri;
847 bool IsICCConditional = IsICCBranch || IsICCMove;
848 if (!IsICCConditional ||
849 (I->getOperand(IsICCBranch ? 1 : 3).getImm() != SPCC::ICC_E &&
850 I->getOperand(IsICCBranch ? 1 : 3).getImm() != SPCC::ICC_NE))
851 return false;
852 } else if (I->modifiesRegister(SP::ICC, TRI)) {
853 IsICCModified = true;
854 break;
855 }
856 }
857
858 if (!IsICCModified) {
859 MachineBasicBlock *MBB = CmpInstr.getParent();
860 if (any_of(MBB->successors(),
861 [](MachineBasicBlock *Succ) { return Succ->isLiveIn(SP::ICC); }))
862 return false;
863 }
864
865 if (MRI->hasOneNonDBGUse(SrcReg))
866 MI->getOperand(0).setReg(SP::G0);
867
868 MI->setDesc(get(NewOpcode));
869 MI->addRegisterDefined(SP::ICC);
870 CmpInstr.eraseFromParent();
871
872 return true;
873}
874
876 switch (MI.getOpcode()) {
877 case TargetOpcode::LOAD_STACK_GUARD: {
878 assert(Subtarget.getTargetTriple().isOSLinux() &&
879 "Only Linux target is expected to contain LOAD_STACK_GUARD");
880 // offsetof(tcbhead_t, stack_guard) from sysdeps/sparc/nptl/tls.h in glibc.
881 const int64_t Offset = Subtarget.is64Bit() ? 0x28 : 0x14;
882 MI.setDesc(get(Subtarget.is64Bit() ? SP::LDXri : SP::LDri));
883 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
884 .addReg(SP::G7)
885 .addImm(Offset);
886 return true;
887 }
888 case SP::V8BAR: {
889 assert(!Subtarget.isV9() &&
890 "V8BAR should not be emitted on V9 processors!");
891
892 // Emit stbar; ldstub [%sp-1], %g0
893 // The sequence acts as a full barrier on V8 systems.
894 MachineBasicBlock &MBB = *MI.getParent();
895 MachineInstr &InstSTBAR =
896 *BuildMI(MBB, MI, MI.getDebugLoc(), get(SP::STBAR));
897 MachineInstr &InstLDSTUB =
898 *BuildMI(MBB, MI, MI.getDebugLoc(), get(SP::LDSTUBri), SP::G0)
899 .addReg(SP::O6)
900 .addImm(-1);
901 MIBundleBuilder(MBB, InstSTBAR, InstLDSTUB);
902 MBB.erase(MI);
903 return true;
904 }
905 }
906 return false;
907}
static void parseCondBranch(MachineInstr *LastInst, MachineBasicBlock *&Target, SmallVectorImpl< MachineOperand > &Cond)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:484
This file defines the SmallVector class.
static bool isFCondBranchOpcode(int Opc)
static bool isRegCondBranchOpcode(int Opc)
static SPCC::CondCodes GetOppositeBranchCondition(SPCC::CondCodes CC)
static cl::opt< unsigned > BPrDisplacementBits("sparc-bpr-offset-bits", cl::Hidden, cl::init(16), cl::desc("Restrict range of BPr instructions (DEBUG)"))
static bool isI32CondBranchOpcode(int Opc)
static cl::opt< unsigned > BPccDisplacementBits("sparc-bpcc-offset-bits", cl::Hidden, cl::init(19), cl::desc("Restrict range of BPcc/FBPfcc instructions (DEBUG)"))
static bool isI64CondBranchOpcode(int Opc)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A debug info location.
Definition DebugLoc.h:126
Helper class for constructing bundles of MachineInstrs.
MachineInstrBundleIterator< MachineInstr > iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LLT MemTy, Align BaseAlignment, const MMOMetadata &Metadata=MMOMetadata(), SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
LLVM_ABI bool addRegisterKilled(Register IncomingReg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI kills a register.
LLVM_ABI void addRegisterDefined(Register Reg, const TargetRegisterInfo *RegInfo=nullptr)
We have determined MI defines a register.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
A description of a memory reference used in the backend.
@ MOLoad
The memory access reads data.
@ MOStore
The memory access writes data.
MachineOperand class - Representation of each machine instruction operand.
int64_t getImm() const
MachineBasicBlock * getMBB() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
static MachineOperand CreateImm(int64_t Val)
Register getReg() const
getReg - Returns the register number.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI bool hasOneNonDBGUse(Register RegNo) const
hasOneNonDBGUse - Return true if there is exactly one non-Debug use of the specified register.
LLVM_ABI LLVM_READONLY MachineInstr * getUniqueVRegDef(Register Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
bool needsUnimp(const MachineInstr &MI, unsigned &StructSize) const
Register isLoadFromStackSlot(const MachineInstr &MI, int &FrameIndex, TypeSize &MemBytes) const override
isLoadFromStackSlot - If the specified machine instruction is a direct load from a stack slot,...
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, const DebugLoc &DL, Register DestReg, Register SrcReg, bool KillSrc, bool RenamableDest=false, bool RenamableSrc=false) const override
bool isBranchOffsetInRange(unsigned BranchOpc, int64_t Offset) const override
Determine if the branch target is in range.
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
bool analyzeCompare(const MachineInstr &MI, Register &SrcReg, Register &SrcReg2, int64_t &CmpMask, int64_t &CmpValue) const override
bool optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg, Register SrcReg2, int64_t CmpMask, int64_t CmpValue, const MachineRegisterInfo *MRI) const override
bool reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const override
Register getGlobalBaseReg(MachineFunction *MF) const
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DestReg, int FrameIndex, const TargetRegisterClass *RC, Register VReg, unsigned SubReg=0, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const override
MachineBasicBlock * getBranchDestBlock(const MachineInstr &MI) const override
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify=false) const override
Register isStoreToStackSlot(const MachineInstr &MI, int &FrameIndex, TypeSize &MemBytes) const override
isStoreToStackSlot - If the specified machine instruction is a direct store to a stack slot,...
SparcInstrInfo(const SparcSubtarget &ST)
const SparcRegisterInfo & getRegisterInfo() const
getRegisterInfo - TargetInstrInfo is a superset of MRegister info.
const TargetRegisterClass * getInlineAsmMemoryOperandRegClass(InlineAsm::ConstraintCode C) const override
unsigned getInstSizeInBytes(const MachineInstr &MI) const override
GetInstSize - Return the number of bytes of code the specified instruction may be.
unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &DL, int *BytesAdded=nullptr) const override
bool expandPostRAPseudo(MachineInstr &MI) const override
Primary interface to the complete machine description for the target machine.
bool isPositionIndependent() const
const MCAsmInfo & getMCAsmInfo() const
Return target specific asm information.
CodeModel::Model getCodeModel() const
Returns the code model.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Target - Wrapper for Target specific information.
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:339
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ CPCC_03
Definition Sparc.h:88
@ CPCC_013
Definition Sparc.h:92
@ FCC_ULE
Definition Sparc.h:74
@ ICC_POS
Definition Sparc.h:54
@ FCC_UG
Definition Sparc.h:64
@ ICC_N
Definition Sparc.h:43
@ CPCC_123
Definition Sparc.h:86
@ ICC_G
Definition Sparc.h:46
@ REG_LEZ
Definition Sparc.h:97
@ CPCC_23
Definition Sparc.h:82
@ ICC_L
Definition Sparc.h:49
@ REG_BEGIN
Definition Sparc.h:95
@ FCC_NE
Definition Sparc.h:68
@ CPCC_01
Definition Sparc.h:91
@ CPCC_12
Definition Sparc.h:85
@ ICC_CS
Definition Sparc.h:53
@ FCC_LG
Definition Sparc.h:67
@ ICC_VS
Definition Sparc.h:57
@ CPCC_1
Definition Sparc.h:83
@ ICC_LEU
Definition Sparc.h:51
@ CPCC_A
Definition Sparc.h:78
@ CPCC_0
Definition Sparc.h:87
@ FCC_LE
Definition Sparc.h:73
@ CPCC_012
Definition Sparc.h:93
@ ICC_LE
Definition Sparc.h:47
@ FCC_U
Definition Sparc.h:62
@ CPCC_2
Definition Sparc.h:81
@ FCC_A
Definition Sparc.h:60
@ CPCC_023
Definition Sparc.h:90
@ CPCC_13
Definition Sparc.h:84
@ ICC_GE
Definition Sparc.h:48
@ FCC_E
Definition Sparc.h:69
@ CPCC_02
Definition Sparc.h:89
@ REG_LZ
Definition Sparc.h:98
@ FCC_L
Definition Sparc.h:65
@ ICC_GU
Definition Sparc.h:50
@ FCC_O
Definition Sparc.h:75
@ ICC_NE
Definition Sparc.h:44
@ FCC_UE
Definition Sparc.h:70
@ REG_NZ
Definition Sparc.h:99
@ ICC_E
Definition Sparc.h:45
@ FCC_GE
Definition Sparc.h:71
@ ICC_A
Definition Sparc.h:42
@ CPCC_3
Definition Sparc.h:80
@ FCC_UGE
Definition Sparc.h:72
@ REG_Z
Definition Sparc.h:96
@ ICC_VC
Definition Sparc.h:56
@ ICC_CC
Definition Sparc.h:52
@ REG_GEZ
Definition Sparc.h:101
@ CPCC_N
Definition Sparc.h:79
@ FCC_G
Definition Sparc.h:63
@ FCC_UL
Definition Sparc.h:66
@ FCC_N
Definition Sparc.h:61
@ ICC_NEG
Definition Sparc.h:55
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
static bool isCondBranchOpcode(int Opc)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr RegState getKillRegState(bool B)
static bool isIndirectBranchOpcode(int Opc)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
static bool isUncondBranchOpcode(int Opc)
constexpr bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition MathExtras.h:249
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.