blob: dd541e5d6268076a51a3bea9706f64d04998322c [file] [log] [blame]
Akira Hatanaka23e8ecf2011-05-04 17:54:27 +00001//===-- MipsEmitGPRestore.cpp - Emit GP restore instruction-----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===-----------------------------------------------------------------------===//
9//
10// This pass emits instructions that restore $gp right
11// after jalr instructions.
12//
13//===-----------------------------------------------------------------------===//
14
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
41 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
42 bool runOnMachineFunction(MachineFunction &F);
43 };
44 char Inserter::ID = 0;
45} // end of anonymous namespace
46
47bool Inserter::runOnMachineFunction(MachineFunction &F) {
48 if (TM.getRelocationModel() != Reloc::PIC_)
49 return false;
50
51 bool Changed = false;
52 int FI = F.getInfo<MipsFunctionInfo>()->getGPFI();
53
54 for (MachineFunction::iterator MFI = F.begin(), MFE = F.end();
55 MFI != MFE; ++MFI) {
56 MachineBasicBlock& MBB = *MFI;
57 MachineBasicBlock::iterator I = MFI->begin();
58
59 while (I != MFI->end()) {
60 if (I->getOpcode() != Mips::JALR) {
61 ++I;
62 continue;
63 }
64
65 DebugLoc dl = I->getDebugLoc();
66 // emit lw $gp, ($gp save slot on stack) after jalr
67 BuildMI(MBB, ++I, dl, TII->get(Mips::LW), Mips::GP).addImm(0).addFrameIndex(FI);
68 Changed = true;
69 }
70 }
71
72 return Changed;
73}
74
75/// createMipsEmitGPRestorePass - Returns a pass that emits instructions that
76/// restores $gp clobbered by jalr instructions.
77FunctionPass *llvm::createMipsEmitGPRestorePass(MipsTargetMachine &tm) {
78 return new Inserter(tm);
79}
80