blob: 119d1a8246881c493e7fcb91b75ba19ef9083135 [file] [log] [blame]
Jia Liuc5707112012-02-17 08:55:11 +00001//===-- MipsEmitGPRestore.cpp - Emit GP Restore Instruction ---------------===//
Akira Hatanaka6b7588e2011-05-04 17:54:27 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
Akira Hatanaka325e66d2011-05-06 22:11:29 +00008//===----------------------------------------------------------------------===//
Akira Hatanaka6b7588e2011-05-04 17:54:27 +00009//
10// This pass emits instructions that restore $gp right
11// after jalr instructions.
12//
Akira Hatanaka325e66d2011-05-06 22:11:29 +000013//===----------------------------------------------------------------------===//
Akira Hatanaka6b7588e2011-05-04 17:54:27 +000014
15#define DEBUG_TYPE "emit-gp-restore"
16
17#include "Mips.h"
18#include "MipsTargetMachine.h"
19#include "MipsMachineFunction.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/Target/TargetInstrInfo.h"
23#include "llvm/ADT/Statistic.h"
24
25using namespace llvm;
26
27namespace {
28 struct Inserter : public MachineFunctionPass {
29
30 TargetMachine &TM;
31 const TargetInstrInfo *TII;
32
33 static char ID;
34 Inserter(TargetMachine &tm)
35 : MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
36
37 virtual const char *getPassName() const {
38 return "Mips Emit GP Restore";
39 }
40
Akira Hatanaka6b7588e2011-05-04 17:54:27 +000041 bool runOnMachineFunction(MachineFunction &F);
42 };
43 char Inserter::ID = 0;
44} // end of anonymous namespace
45
46bool Inserter::runOnMachineFunction(MachineFunction &F) {
Akira Hatanaka648f00c2012-02-24 22:34:47 +000047 MipsFunctionInfo *MipsFI = F.getInfo<MipsFunctionInfo>();
48
49 if ((TM.getRelocationModel() != Reloc::PIC_) ||
50 (!MipsFI->globalBaseRegFixed()))
Akira Hatanaka6b7588e2011-05-04 17:54:27 +000051 return false;
52
53 bool Changed = false;
Akira Hatanaka648f00c2012-02-24 22:34:47 +000054 int FI = MipsFI->getGPFI();
Akira Hatanaka6b7588e2011-05-04 17:54:27 +000055
56 for (MachineFunction::iterator MFI = F.begin(), MFE = F.end();
57 MFI != MFE; ++MFI) {
58 MachineBasicBlock& MBB = *MFI;
59 MachineBasicBlock::iterator I = MFI->begin();
60
Akira Hatanakacf0cd802011-05-26 18:59:03 +000061 // If MBB is a landing pad, insert instruction that restores $gp after
62 // EH_LABEL.
63 if (MBB.isLandingPad()) {
64 // Find EH_LABEL first.
65 for (; I->getOpcode() != TargetOpcode::EH_LABEL; ++I) ;
Jia Liubb481f82012-02-28 07:46:26 +000066
Akira Hatanakacf0cd802011-05-26 18:59:03 +000067 // Insert lw.
68 ++I;
69 DebugLoc dl = I != MBB.end() ? I->getDebugLoc() : DebugLoc();
Akira Hatanakad3ac47f2011-07-07 18:57:00 +000070 BuildMI(MBB, I, dl, TII->get(Mips::LW), Mips::GP).addFrameIndex(FI)
71 .addImm(0);
Akira Hatanakacf0cd802011-05-26 18:59:03 +000072 Changed = true;
73 }
74
Akira Hatanaka6b7588e2011-05-04 17:54:27 +000075 while (I != MFI->end()) {
76 if (I->getOpcode() != Mips::JALR) {
77 ++I;
78 continue;
79 }
80
81 DebugLoc dl = I->getDebugLoc();
82 // emit lw $gp, ($gp save slot on stack) after jalr
Akira Hatanakad3ac47f2011-07-07 18:57:00 +000083 BuildMI(MBB, ++I, dl, TII->get(Mips::LW), Mips::GP).addFrameIndex(FI)
84 .addImm(0);
Akira Hatanaka6b7588e2011-05-04 17:54:27 +000085 Changed = true;
86 }
Jia Liubb481f82012-02-28 07:46:26 +000087 }
Akira Hatanaka6b7588e2011-05-04 17:54:27 +000088
89 return Changed;
90}
91
92/// createMipsEmitGPRestorePass - Returns a pass that emits instructions that
93/// restores $gp clobbered by jalr instructions.
94FunctionPass *llvm::createMipsEmitGPRestorePass(MipsTargetMachine &tm) {
95 return new Inserter(tm);
96}
97