1 //===- MipsInstrInfo.cpp - Mips Instruction Information ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the Mips implementation of the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "MipsInstrInfo.h"
15 #include "MipsTargetMachine.h"
16 #include "MipsMachineFunction.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "MipsGenInstrInfo.inc"
25 MipsInstrInfo::MipsInstrInfo(MipsTargetMachine &tm)
26 : TargetInstrInfoImpl(MipsInsts, array_lengthof(MipsInsts)),
27 TM(tm), RI(*TM.getSubtargetImpl(), *this) {}
29 static bool isZeroImm(const MachineOperand &op) {
30 return op.isImm() && op.getImm() == 0;
33 /// Return true if the instruction is a register to register move and
34 /// leave the source and dest operands in the passed parameters.
36 isMoveInstr(const MachineInstr &MI, unsigned &SrcReg, unsigned &DstReg,
37 unsigned &SrcSubIdx, unsigned &DstSubIdx) const
39 SrcSubIdx = DstSubIdx = 0; // No sub-registers.
41 // addu $dst, $src, $zero || addu $dst, $zero, $src
42 // or $dst, $src, $zero || or $dst, $zero, $src
43 if ((MI.getOpcode() == Mips::ADDu) || (MI.getOpcode() == Mips::OR)) {
44 if (MI.getOperand(1).getReg() == Mips::ZERO) {
45 DstReg = MI.getOperand(0).getReg();
46 SrcReg = MI.getOperand(2).getReg();
48 } else if (MI.getOperand(2).getReg() == Mips::ZERO) {
49 DstReg = MI.getOperand(0).getReg();
50 SrcReg = MI.getOperand(1).getReg();
58 if (MI.getOpcode() == Mips::FMOV_S32 ||
59 MI.getOpcode() == Mips::FMOV_D32 ||
60 MI.getOpcode() == Mips::MFC1 ||
61 MI.getOpcode() == Mips::MTC1 ||
62 MI.getOpcode() == Mips::MOVCCRToCCR) {
63 DstReg = MI.getOperand(0).getReg();
64 SrcReg = MI.getOperand(1).getReg();
68 // addiu $dst, $src, 0
69 if (MI.getOpcode() == Mips::ADDiu) {
70 if ((MI.getOperand(1).isReg()) && (isZeroImm(MI.getOperand(2)))) {
71 DstReg = MI.getOperand(0).getReg();
72 SrcReg = MI.getOperand(1).getReg();
80 /// isLoadFromStackSlot - If the specified machine instruction is a direct
81 /// load from a stack slot, return the virtual or physical register number of
82 /// the destination along with the FrameIndex of the loaded stack slot. If
83 /// not, return 0. This predicate must return 0 if the instruction has
84 /// any side effects other than loading from the stack slot.
85 unsigned MipsInstrInfo::
86 isLoadFromStackSlot(const MachineInstr *MI, int &FrameIndex) const
88 if ((MI->getOpcode() == Mips::LW) || (MI->getOpcode() == Mips::LWC1) ||
89 (MI->getOpcode() == Mips::LDC1)) {
90 if ((MI->getOperand(2).isFI()) && // is a stack slot
91 (MI->getOperand(1).isImm()) && // the imm is zero
92 (isZeroImm(MI->getOperand(1)))) {
93 FrameIndex = MI->getOperand(2).getIndex();
94 return MI->getOperand(0).getReg();
101 /// isStoreToStackSlot - If the specified machine instruction is a direct
102 /// store to a stack slot, return the virtual or physical register number of
103 /// the source reg along with the FrameIndex of the loaded stack slot. If
104 /// not, return 0. This predicate must return 0 if the instruction has
105 /// any side effects other than storing to the stack slot.
106 unsigned MipsInstrInfo::
107 isStoreToStackSlot(const MachineInstr *MI, int &FrameIndex) const
109 if ((MI->getOpcode() == Mips::SW) || (MI->getOpcode() == Mips::SWC1) ||
110 (MI->getOpcode() == Mips::SDC1)) {
111 if ((MI->getOperand(2).isFI()) && // is a stack slot
112 (MI->getOperand(1).isImm()) && // the imm is zero
113 (isZeroImm(MI->getOperand(1)))) {
114 FrameIndex = MI->getOperand(2).getIndex();
115 return MI->getOperand(0).getReg();
121 /// insertNoop - If data hazard condition is found insert the target nop
124 insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
127 BuildMI(MBB, MI, DL, get(Mips::NOP));
131 copyPhysReg(MachineBasicBlock &MBB,
132 MachineBasicBlock::iterator I, DebugLoc DL,
133 unsigned DestReg, unsigned SrcReg,
134 bool KillSrc) const {
135 bool DestCPU = Mips::CPURegsRegClass.contains(DestReg);
136 bool SrcCPU = Mips::CPURegsRegClass.contains(SrcReg);
138 // CPU-CPU is the most common.
139 if (DestCPU && SrcCPU) {
140 BuildMI(MBB, I, DL, get(Mips::ADDu), DestReg).addReg(Mips::ZERO)
141 .addReg(SrcReg, getKillRegState(KillSrc));
145 // Copy to CPU from other registers.
147 if (Mips::CCRRegClass.contains(SrcReg))
148 BuildMI(MBB, I, DL, get(Mips::CFC1), DestReg)
149 .addReg(SrcReg, getKillRegState(KillSrc));
150 else if (Mips::FGR32RegClass.contains(SrcReg))
151 BuildMI(MBB, I, DL, get(Mips::MFC1), DestReg)
152 .addReg(SrcReg, getKillRegState(KillSrc));
153 else if (SrcReg == Mips::HI)
154 BuildMI(MBB, I, DL, get(Mips::MFHI), DestReg);
155 else if (SrcReg == Mips::LO)
156 BuildMI(MBB, I, DL, get(Mips::MFLO), DestReg);
158 llvm_unreachable("Copy to CPU from invalid register");
162 // Copy to other registers from CPU.
164 if (Mips::CCRRegClass.contains(DestReg))
165 BuildMI(MBB, I, DL, get(Mips::CTC1), DestReg)
166 .addReg(SrcReg, getKillRegState(KillSrc));
167 else if (Mips::FGR32RegClass.contains(DestReg))
168 BuildMI(MBB, I, DL, get(Mips::MTC1), DestReg)
169 .addReg(SrcReg, getKillRegState(KillSrc));
170 else if (DestReg == Mips::HI)
171 BuildMI(MBB, I, DL, get(Mips::MTHI))
172 .addReg(SrcReg, getKillRegState(KillSrc));
173 else if (DestReg == Mips::LO)
174 BuildMI(MBB, I, DL, get(Mips::MTLO))
175 .addReg(SrcReg, getKillRegState(KillSrc));
177 llvm_unreachable("Copy from CPU to invalid register");
181 if (Mips::FGR32RegClass.contains(DestReg, SrcReg)) {
182 BuildMI(MBB, I, DL, get(Mips::FMOV_S32), DestReg)
183 .addReg(SrcReg, getKillRegState(KillSrc));
187 if (Mips::AFGR64RegClass.contains(DestReg, SrcReg)) {
188 BuildMI(MBB, I, DL, get(Mips::FMOV_D32), DestReg)
189 .addReg(SrcReg, getKillRegState(KillSrc));
193 if (Mips::CCRRegClass.contains(DestReg, SrcReg)) {
194 BuildMI(MBB, I, DL, get(Mips::MOVCCRToCCR), DestReg)
195 .addReg(SrcReg, getKillRegState(KillSrc));
198 llvm_unreachable("Cannot copy registers");
202 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
203 unsigned SrcReg, bool isKill, int FI,
204 const TargetRegisterClass *RC,
205 const TargetRegisterInfo *TRI) const {
207 if (I != MBB.end()) DL = I->getDebugLoc();
209 if (RC == Mips::CPURegsRegisterClass)
210 BuildMI(MBB, I, DL, get(Mips::SW)).addReg(SrcReg, getKillRegState(isKill))
211 .addImm(0).addFrameIndex(FI);
212 else if (RC == Mips::FGR32RegisterClass)
213 BuildMI(MBB, I, DL, get(Mips::SWC1)).addReg(SrcReg, getKillRegState(isKill))
214 .addImm(0).addFrameIndex(FI);
215 else if (RC == Mips::AFGR64RegisterClass) {
216 if (!TM.getSubtarget<MipsSubtarget>().isMips1()) {
217 BuildMI(MBB, I, DL, get(Mips::SDC1))
218 .addReg(SrcReg, getKillRegState(isKill))
219 .addImm(0).addFrameIndex(FI);
221 const TargetRegisterInfo *TRI =
222 MBB.getParent()->getTarget().getRegisterInfo();
223 const unsigned *SubSet = TRI->getSubRegisters(SrcReg);
224 BuildMI(MBB, I, DL, get(Mips::SWC1))
225 .addReg(SubSet[0], getKillRegState(isKill))
226 .addImm(0).addFrameIndex(FI);
227 BuildMI(MBB, I, DL, get(Mips::SWC1))
228 .addReg(SubSet[1], getKillRegState(isKill))
229 .addImm(4).addFrameIndex(FI);
232 llvm_unreachable("Register class not handled!");
236 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
237 unsigned DestReg, int FI,
238 const TargetRegisterClass *RC,
239 const TargetRegisterInfo *TRI) const
242 if (I != MBB.end()) DL = I->getDebugLoc();
244 if (RC == Mips::CPURegsRegisterClass)
245 BuildMI(MBB, I, DL, get(Mips::LW), DestReg).addImm(0).addFrameIndex(FI);
246 else if (RC == Mips::FGR32RegisterClass)
247 BuildMI(MBB, I, DL, get(Mips::LWC1), DestReg).addImm(0).addFrameIndex(FI);
248 else if (RC == Mips::AFGR64RegisterClass) {
249 if (!TM.getSubtarget<MipsSubtarget>().isMips1()) {
250 BuildMI(MBB, I, DL, get(Mips::LDC1), DestReg).addImm(0).addFrameIndex(FI);
252 const TargetRegisterInfo *TRI =
253 MBB.getParent()->getTarget().getRegisterInfo();
254 const unsigned *SubSet = TRI->getSubRegisters(DestReg);
255 BuildMI(MBB, I, DL, get(Mips::LWC1), SubSet[0])
256 .addImm(0).addFrameIndex(FI);
257 BuildMI(MBB, I, DL, get(Mips::LWC1), SubSet[1])
258 .addImm(4).addFrameIndex(FI);
261 llvm_unreachable("Register class not handled!");
264 MachineInstr *MipsInstrInfo::
265 foldMemoryOperandImpl(MachineFunction &MF,
267 const SmallVectorImpl<unsigned> &Ops, int FI) const
269 if (Ops.size() != 1) return NULL;
271 MachineInstr *NewMI = NULL;
273 switch (MI->getOpcode()) {
275 if ((MI->getOperand(0).isReg()) &&
276 (MI->getOperand(1).isReg()) &&
277 (MI->getOperand(1).getReg() == Mips::ZERO) &&
278 (MI->getOperand(2).isReg())) {
279 if (Ops[0] == 0) { // COPY -> STORE
280 unsigned SrcReg = MI->getOperand(2).getReg();
281 bool isKill = MI->getOperand(2).isKill();
282 bool isUndef = MI->getOperand(2).isUndef();
283 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::SW))
284 .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
285 .addImm(0).addFrameIndex(FI);
286 } else { // COPY -> LOAD
287 unsigned DstReg = MI->getOperand(0).getReg();
288 bool isDead = MI->getOperand(0).isDead();
289 bool isUndef = MI->getOperand(0).isUndef();
290 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Mips::LW))
291 .addReg(DstReg, RegState::Define | getDeadRegState(isDead) |
292 getUndefRegState(isUndef))
293 .addImm(0).addFrameIndex(FI);
299 if ((MI->getOperand(0).isReg()) &&
300 (MI->getOperand(1).isReg())) {
301 const TargetRegisterClass
302 *RC = RI.getRegClass(MI->getOperand(0).getReg());
303 unsigned StoreOpc, LoadOpc;
304 bool IsMips1 = TM.getSubtarget<MipsSubtarget>().isMips1();
306 if (RC == Mips::FGR32RegisterClass) {
307 LoadOpc = Mips::LWC1; StoreOpc = Mips::SWC1;
309 assert(RC == Mips::AFGR64RegisterClass);
310 // Mips1 doesn't have ldc/sdc instructions.
312 LoadOpc = Mips::LDC1; StoreOpc = Mips::SDC1;
315 if (Ops[0] == 0) { // COPY -> STORE
316 unsigned SrcReg = MI->getOperand(1).getReg();
317 bool isKill = MI->getOperand(1).isKill();
318 bool isUndef = MI->getOperand(2).isUndef();
319 NewMI = BuildMI(MF, MI->getDebugLoc(), get(StoreOpc))
320 .addReg(SrcReg, getKillRegState(isKill) | getUndefRegState(isUndef))
321 .addImm(0).addFrameIndex(FI) ;
322 } else { // COPY -> LOAD
323 unsigned DstReg = MI->getOperand(0).getReg();
324 bool isDead = MI->getOperand(0).isDead();
325 bool isUndef = MI->getOperand(0).isUndef();
326 NewMI = BuildMI(MF, MI->getDebugLoc(), get(LoadOpc))
327 .addReg(DstReg, RegState::Define | getDeadRegState(isDead) |
328 getUndefRegState(isUndef))
329 .addImm(0).addFrameIndex(FI);
338 //===----------------------------------------------------------------------===//
340 //===----------------------------------------------------------------------===//
342 /// GetCondFromBranchOpc - Return the Mips CC that matches
343 /// the correspondent Branch instruction opcode.
344 static Mips::CondCode GetCondFromBranchOpc(unsigned BrOpc)
347 default: return Mips::COND_INVALID;
348 case Mips::BEQ : return Mips::COND_E;
349 case Mips::BNE : return Mips::COND_NE;
350 case Mips::BGTZ : return Mips::COND_GZ;
351 case Mips::BGEZ : return Mips::COND_GEZ;
352 case Mips::BLTZ : return Mips::COND_LZ;
353 case Mips::BLEZ : return Mips::COND_LEZ;
355 // We dont do fp branch analysis yet!
357 case Mips::BC1F : return Mips::COND_INVALID;
361 /// GetCondBranchFromCond - Return the Branch instruction
362 /// opcode that matches the cc.
363 unsigned Mips::GetCondBranchFromCond(Mips::CondCode CC)
366 default: llvm_unreachable("Illegal condition code!");
367 case Mips::COND_E : return Mips::BEQ;
368 case Mips::COND_NE : return Mips::BNE;
369 case Mips::COND_GZ : return Mips::BGTZ;
370 case Mips::COND_GEZ : return Mips::BGEZ;
371 case Mips::COND_LZ : return Mips::BLTZ;
372 case Mips::COND_LEZ : return Mips::BLEZ;
377 case Mips::FCOND_UEQ:
378 case Mips::FCOND_OLT:
379 case Mips::FCOND_ULT:
380 case Mips::FCOND_OLE:
381 case Mips::FCOND_ULE:
383 case Mips::FCOND_NGLE:
384 case Mips::FCOND_SEQ:
385 case Mips::FCOND_NGL:
387 case Mips::FCOND_NGE:
389 case Mips::FCOND_NGT: return Mips::BC1T;
393 case Mips::FCOND_NEQ:
394 case Mips::FCOND_OGL:
395 case Mips::FCOND_UGE:
396 case Mips::FCOND_OGE:
397 case Mips::FCOND_UGT:
398 case Mips::FCOND_OGT:
400 case Mips::FCOND_GLE:
401 case Mips::FCOND_SNE:
403 case Mips::FCOND_NLT:
405 case Mips::FCOND_NLE:
406 case Mips::FCOND_GT: return Mips::BC1F;
410 /// GetOppositeBranchCondition - Return the inverse of the specified
411 /// condition, e.g. turning COND_E to COND_NE.
412 Mips::CondCode Mips::GetOppositeBranchCondition(Mips::CondCode CC)
415 default: llvm_unreachable("Illegal condition code!");
416 case Mips::COND_E : return Mips::COND_NE;
417 case Mips::COND_NE : return Mips::COND_E;
418 case Mips::COND_GZ : return Mips::COND_LEZ;
419 case Mips::COND_GEZ : return Mips::COND_LZ;
420 case Mips::COND_LZ : return Mips::COND_GEZ;
421 case Mips::COND_LEZ : return Mips::COND_GZ;
422 case Mips::FCOND_F : return Mips::FCOND_T;
423 case Mips::FCOND_UN : return Mips::FCOND_OR;
424 case Mips::FCOND_EQ : return Mips::FCOND_NEQ;
425 case Mips::FCOND_UEQ: return Mips::FCOND_OGL;
426 case Mips::FCOND_OLT: return Mips::FCOND_UGE;
427 case Mips::FCOND_ULT: return Mips::FCOND_OGE;
428 case Mips::FCOND_OLE: return Mips::FCOND_UGT;
429 case Mips::FCOND_ULE: return Mips::FCOND_OGT;
430 case Mips::FCOND_SF: return Mips::FCOND_ST;
431 case Mips::FCOND_NGLE:return Mips::FCOND_GLE;
432 case Mips::FCOND_SEQ: return Mips::FCOND_SNE;
433 case Mips::FCOND_NGL: return Mips::FCOND_GL;
434 case Mips::FCOND_LT: return Mips::FCOND_NLT;
435 case Mips::FCOND_NGE: return Mips::FCOND_GE;
436 case Mips::FCOND_LE: return Mips::FCOND_NLE;
437 case Mips::FCOND_NGT: return Mips::FCOND_GT;
441 bool MipsInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
442 MachineBasicBlock *&TBB,
443 MachineBasicBlock *&FBB,
444 SmallVectorImpl<MachineOperand> &Cond,
445 bool AllowModify) const
447 // If the block has no terminators, it just falls into the block after it.
448 MachineBasicBlock::iterator I = MBB.end();
449 if (I == MBB.begin())
452 while (I->isDebugValue()) {
453 if (I == MBB.begin())
457 if (!isUnpredicatedTerminator(I))
460 // Get the last instruction in the block.
461 MachineInstr *LastInst = I;
463 // If there is only one terminator instruction, process it.
464 unsigned LastOpc = LastInst->getOpcode();
465 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
466 if (!LastInst->getDesc().isBranch())
469 // Unconditional branch
470 if (LastOpc == Mips::J) {
471 TBB = LastInst->getOperand(0).getMBB();
475 Mips::CondCode BranchCode = GetCondFromBranchOpc(LastInst->getOpcode());
476 if (BranchCode == Mips::COND_INVALID)
477 return true; // Can't handle indirect branch.
479 // Conditional branch
480 // Block ends with fall-through condbranch.
481 if (LastOpc != Mips::COND_INVALID) {
482 int LastNumOp = LastInst->getNumOperands();
484 TBB = LastInst->getOperand(LastNumOp-1).getMBB();
485 Cond.push_back(MachineOperand::CreateImm(BranchCode));
487 for (int i=0; i<LastNumOp-1; i++) {
488 Cond.push_back(LastInst->getOperand(i));
495 // Get the instruction before it if it is a terminator.
496 MachineInstr *SecondLastInst = I;
498 // If there are three terminators, we don't know what sort of block this is.
499 if (SecondLastInst && I != MBB.begin() && isUnpredicatedTerminator(--I))
502 // If the block ends with Mips::J and a Mips::BNE/Mips::BEQ, handle it.
503 unsigned SecondLastOpc = SecondLastInst->getOpcode();
504 Mips::CondCode BranchCode = GetCondFromBranchOpc(SecondLastOpc);
506 if (BranchCode != Mips::COND_INVALID && LastOpc == Mips::J) {
507 int SecondNumOp = SecondLastInst->getNumOperands();
509 TBB = SecondLastInst->getOperand(SecondNumOp-1).getMBB();
510 Cond.push_back(MachineOperand::CreateImm(BranchCode));
512 for (int i=0; i<SecondNumOp-1; i++) {
513 Cond.push_back(SecondLastInst->getOperand(i));
516 FBB = LastInst->getOperand(0).getMBB();
520 // If the block ends with two unconditional branches, handle it. The last
521 // one is not executed, so remove it.
522 if ((SecondLastOpc == Mips::J) && (LastOpc == Mips::J)) {
523 TBB = SecondLastInst->getOperand(0).getMBB();
526 I->eraseFromParent();
530 // Otherwise, can't handle this.
534 unsigned MipsInstrInfo::
535 InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
536 MachineBasicBlock *FBB,
537 const SmallVectorImpl<MachineOperand> &Cond,
539 // Shouldn't be a fall through.
540 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
541 assert((Cond.size() == 3 || Cond.size() == 2 || Cond.size() == 0) &&
542 "Mips branch conditions can have two|three components!");
544 if (FBB == 0) { // One way branch.
546 // Unconditional branch?
547 BuildMI(&MBB, DL, get(Mips::J)).addMBB(TBB);
549 // Conditional branch.
550 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
551 const TargetInstrDesc &TID = get(Opc);
553 if (TID.getNumOperands() == 3)
554 BuildMI(&MBB, DL, TID).addReg(Cond[1].getReg())
555 .addReg(Cond[2].getReg())
558 BuildMI(&MBB, DL, TID).addReg(Cond[1].getReg())
565 // Two-way Conditional branch.
566 unsigned Opc = GetCondBranchFromCond((Mips::CondCode)Cond[0].getImm());
567 const TargetInstrDesc &TID = get(Opc);
569 if (TID.getNumOperands() == 3)
570 BuildMI(&MBB, DL, TID).addReg(Cond[1].getReg()).addReg(Cond[2].getReg())
573 BuildMI(&MBB, DL, TID).addReg(Cond[1].getReg()).addMBB(TBB);
575 BuildMI(&MBB, DL, get(Mips::J)).addMBB(FBB);
579 unsigned MipsInstrInfo::
580 RemoveBranch(MachineBasicBlock &MBB) const
582 MachineBasicBlock::iterator I = MBB.end();
583 if (I == MBB.begin()) return 0;
585 while (I->isDebugValue()) {
586 if (I == MBB.begin())
590 if (I->getOpcode() != Mips::J &&
591 GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
594 // Remove the branch.
595 I->eraseFromParent();
599 if (I == MBB.begin()) return 1;
601 if (GetCondFromBranchOpc(I->getOpcode()) == Mips::COND_INVALID)
604 // Remove the branch.
605 I->eraseFromParent();
609 /// ReverseBranchCondition - Return the inverse opcode of the
610 /// specified Branch instruction.
612 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const
614 assert( (Cond.size() == 3 || Cond.size() == 2) &&
615 "Invalid Mips branch condition!");
616 Cond[0].setImm(GetOppositeBranchCondition((Mips::CondCode)Cond[0].getImm()));
620 /// getGlobalBaseReg - Return a virtual register initialized with the
621 /// the global base register value. Output instructions required to
622 /// initialize the register in the function entry block, if necessary.
624 unsigned MipsInstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
625 MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
626 unsigned GlobalBaseReg = MipsFI->getGlobalBaseReg();
627 if (GlobalBaseReg != 0)
628 return GlobalBaseReg;
630 // Insert the set of GlobalBaseReg into the first MBB of the function
631 MachineBasicBlock &FirstMBB = MF->front();
632 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
633 MachineRegisterInfo &RegInfo = MF->getRegInfo();
634 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
636 GlobalBaseReg = RegInfo.createVirtualRegister(Mips::CPURegsRegisterClass);
637 BuildMI(FirstMBB, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY),
638 GlobalBaseReg).addReg(Mips::GP);
639 RegInfo.addLiveIn(Mips::GP);
641 MipsFI->setGlobalBaseReg(GlobalBaseReg);
642 return GlobalBaseReg;