blob: 965d482acfe8948931fa8a71a0988ac281ffc752 [file] [log] [blame]
Tony Linthicumb4b54152011-12-12 21:14:40 +00001//===--- HexagonExpandPredSpillCode.cpp - Expand Predicate Spill Code ----===//
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// The Hexagon processor has no instructions that load or store predicate
10// registers directly. So, when these registers must be spilled a general
11// purpose register must be found and the value copied to/from it from/to
12// the predicate register. This code currently does not use the register
13// scavenger mechanism available in the allocator. There are two registers
14// reserved to allow spilling/restoring predicate registers. One is used to
15// hold the predicate value. The other is used when stack frame offsets are
16// too large.
17//
18//===----------------------------------------------------------------------===//
19
20
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/CodeGen/LatencyPriorityQueue.h"
23#include "llvm/CodeGen/SchedulerRegistry.h"
24#include "llvm/CodeGen/MachineDominators.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineLoopInfo.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
28#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
29#include "llvm/Target/TargetMachine.h"
30#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetRegisterInfo.h"
32#include "llvm/Support/Compiler.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/ADT/Statistic.h"
35#include "llvm/Support/MathExtras.h"
36#include "llvm/CodeGen/MachineInstrBuilder.h"
37#include "HexagonTargetMachine.h"
38#include "HexagonSubtarget.h"
39#include "HexagonMachineFunctionInfo.h"
40#include <map>
41#include <iostream>
42
43#include "llvm/Support/CommandLine.h"
44
45
46using namespace llvm;
47
48
49namespace {
50
51class HexagonExpandPredSpillCode : public MachineFunctionPass {
52 HexagonTargetMachine& QTM;
53 const HexagonSubtarget &QST;
54
55 public:
56 static char ID;
57 HexagonExpandPredSpillCode(HexagonTargetMachine& TM) :
58 MachineFunctionPass(ID), QTM(TM), QST(*TM.getSubtargetImpl()) {}
59
60 const char *getPassName() const {
61 return "Hexagon Expand Predicate Spill Code";
62 }
63 bool runOnMachineFunction(MachineFunction &Fn);
64};
65
66
67char HexagonExpandPredSpillCode::ID = 0;
68
69
70bool HexagonExpandPredSpillCode::runOnMachineFunction(MachineFunction &Fn) {
71
72 const HexagonInstrInfo *TII = QTM.getInstrInfo();
Tony Linthicumb4b54152011-12-12 21:14:40 +000073
74 // Loop over all of the basic blocks.
75 for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end();
76 MBBb != MBBe; ++MBBb) {
77 MachineBasicBlock* MBB = MBBb;
78 // Traverse the basic block.
79 for (MachineBasicBlock::iterator MII = MBB->begin(); MII != MBB->end();
80 ++MII) {
81 MachineInstr *MI = MII;
82 int Opc = MI->getOpcode();
83 if (Opc == Hexagon::STriw_pred) {
84 // STriw_pred [R30], ofst, SrcReg;
85 unsigned FP = MI->getOperand(0).getReg();
Benjamin Kramer27baab62011-12-27 11:41:05 +000086 assert(FP == QTM.getRegisterInfo()->getFrameRegister() &&
Tony Linthicumb4b54152011-12-12 21:14:40 +000087 "Not a Frame Pointer, Nor a Spill Slot");
88 assert(MI->getOperand(1).isImm() && "Not an offset");
89 int Offset = MI->getOperand(1).getImm();
90 int SrcReg = MI->getOperand(2).getReg();
91 assert(Hexagon::PredRegsRegClass.contains(SrcReg) &&
92 "Not a predicate register");
93 if (!TII->isValidOffset(Hexagon::STriw, Offset)) {
94 if (!TII->isValidOffset(Hexagon::ADD_ri, Offset)) {
95 BuildMI(*MBB, MII, MI->getDebugLoc(),
96 TII->get(Hexagon::CONST32_Int_Real),
97 HEXAGON_RESERVED_REG_1).addImm(Offset);
98 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::ADD_rr),
99 HEXAGON_RESERVED_REG_1)
100 .addReg(FP).addReg(HEXAGON_RESERVED_REG_1);
101 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::TFR_RsPd),
102 HEXAGON_RESERVED_REG_2).addReg(SrcReg);
103 BuildMI(*MBB, MII, MI->getDebugLoc(),
104 TII->get(Hexagon::STriw))
105 .addReg(HEXAGON_RESERVED_REG_1)
106 .addImm(0).addReg(HEXAGON_RESERVED_REG_2);
107 } else {
108 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::ADD_ri),
109 HEXAGON_RESERVED_REG_1).addReg(FP).addImm(Offset);
110 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::TFR_RsPd),
111 HEXAGON_RESERVED_REG_2).addReg(SrcReg);
112 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::STriw))
113 .addReg(HEXAGON_RESERVED_REG_1)
114 .addImm(0)
115 .addReg(HEXAGON_RESERVED_REG_2);
116 }
117 } else {
118 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::TFR_RsPd),
119 HEXAGON_RESERVED_REG_2).addReg(SrcReg);
120 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::STriw)).
121 addReg(FP).addImm(Offset).addReg(HEXAGON_RESERVED_REG_2);
122 }
123 MII = MBB->erase(MI);
124 --MII;
125 } else if (Opc == Hexagon::LDriw_pred) {
126 // DstReg = LDriw_pred [R30], ofst.
127 int DstReg = MI->getOperand(0).getReg();
128 assert(Hexagon::PredRegsRegClass.contains(DstReg) &&
129 "Not a predicate register");
130 unsigned FP = MI->getOperand(1).getReg();
Benjamin Kramer27baab62011-12-27 11:41:05 +0000131 assert(FP == QTM.getRegisterInfo()->getFrameRegister() &&
Tony Linthicumb4b54152011-12-12 21:14:40 +0000132 "Not a Frame Pointer, Nor a Spill Slot");
133 assert(MI->getOperand(2).isImm() && "Not an offset");
134 int Offset = MI->getOperand(2).getImm();
135 if (!TII->isValidOffset(Hexagon::LDriw, Offset)) {
136 if (!TII->isValidOffset(Hexagon::ADD_ri, Offset)) {
137 BuildMI(*MBB, MII, MI->getDebugLoc(),
138 TII->get(Hexagon::CONST32_Int_Real),
139 HEXAGON_RESERVED_REG_1).addImm(Offset);
140 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::ADD_rr),
141 HEXAGON_RESERVED_REG_1)
142 .addReg(FP)
143 .addReg(HEXAGON_RESERVED_REG_1);
144 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::LDriw),
145 HEXAGON_RESERVED_REG_2)
146 .addReg(HEXAGON_RESERVED_REG_1)
147 .addImm(0);
148 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::TFR_PdRs),
149 DstReg).addReg(HEXAGON_RESERVED_REG_2);
150 } else {
151 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::ADD_ri),
152 HEXAGON_RESERVED_REG_1).addReg(FP).addImm(Offset);
153 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::LDriw),
154 HEXAGON_RESERVED_REG_2)
155 .addReg(HEXAGON_RESERVED_REG_1)
156 .addImm(0);
157 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::TFR_PdRs),
158 DstReg).addReg(HEXAGON_RESERVED_REG_2);
159 }
160 } else {
161 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::LDriw),
162 HEXAGON_RESERVED_REG_2).addReg(FP).addImm(Offset);
163 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::TFR_PdRs),
164 DstReg).addReg(HEXAGON_RESERVED_REG_2);
165 }
166 MII = MBB->erase(MI);
167 --MII;
168 }
169 }
170 }
171
172 return true;
173}
174
175}
176
177//===----------------------------------------------------------------------===//
178// Public Constructor Functions
179//===----------------------------------------------------------------------===//
180
181FunctionPass *llvm::createHexagonExpandPredSpillCode(HexagonTargetMachine &TM) {
182 return new HexagonExpandPredSpillCode(TM);
183}