blob: 8ef4c3ad74e2e7f367c5e9a80781f768b8d06544 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonExpandPredSpillCode.cpp - Expand Predicate Spill Code ------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +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//
Jia Liub22310f2012-02-18 12:03:15 +00008//===----------------------------------------------------------------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00009// The Hexagon processor has no instructions that load or store predicate
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000010// 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
Tony Linthicum1213a7a2011-12-12 21:14:40 +000013// 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
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "Hexagon.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000021#include "HexagonMachineFunctionInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "HexagonSubtarget.h"
23#include "HexagonTargetMachine.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000024#include "llvm/ADT/Statistic.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025#include "llvm/CodeGen/LatencyPriorityQueue.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000026#include "llvm/CodeGen/MachineDominators.h"
27#include "llvm/CodeGen/MachineFunctionPass.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000028#include "llvm/CodeGen/MachineInstrBuilder.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000029#include "llvm/CodeGen/MachineLoopInfo.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000031#include "llvm/CodeGen/Passes.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000032#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000033#include "llvm/CodeGen/SchedulerRegistry.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000034#include "llvm/Support/Compiler.h"
35#include "llvm/Support/Debug.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000036#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000037#include "llvm/Target/TargetInstrInfo.h"
38#include "llvm/Target/TargetMachine.h"
39#include "llvm/Target/TargetRegisterInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000040
41using namespace llvm;
42
43
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000044namespace llvm {
45 void initializeHexagonExpandPredSpillCodePass(PassRegistry&);
46}
47
48
Tony Linthicum1213a7a2011-12-12 21:14:40 +000049namespace {
50
51class HexagonExpandPredSpillCode : public MachineFunctionPass {
Krzysztof Parzyszekd5007472013-05-06 18:38:37 +000052 const HexagonTargetMachine& QTM;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000053 const HexagonSubtarget &QST;
54
55 public:
56 static char ID;
Krzysztof Parzyszekd5007472013-05-06 18:38:37 +000057 HexagonExpandPredSpillCode(const HexagonTargetMachine& TM) :
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +000058 MachineFunctionPass(ID), QTM(TM), QST(*TM.getSubtargetImpl()) {
59 PassRegistry &Registry = *PassRegistry::getPassRegistry();
60 initializeHexagonExpandPredSpillCodePass(Registry);
61 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +000062
Craig Topper906c2cd2014-04-29 07:58:16 +000063 const char *getPassName() const override {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000064 return "Hexagon Expand Predicate Spill Code";
65 }
Craig Topper906c2cd2014-04-29 07:58:16 +000066 bool runOnMachineFunction(MachineFunction &Fn) override;
Tony Linthicum1213a7a2011-12-12 21:14:40 +000067};
68
69
70char HexagonExpandPredSpillCode::ID = 0;
71
72
73bool HexagonExpandPredSpillCode::runOnMachineFunction(MachineFunction &Fn) {
74
Eric Christopherd9134482014-08-04 21:25:23 +000075 const HexagonInstrInfo *TII = QTM.getSubtargetImpl()->getInstrInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +000076
77 // Loop over all of the basic blocks.
78 for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end();
79 MBBb != MBBe; ++MBBb) {
80 MachineBasicBlock* MBB = MBBb;
81 // Traverse the basic block.
82 for (MachineBasicBlock::iterator MII = MBB->begin(); MII != MBB->end();
83 ++MII) {
84 MachineInstr *MI = MII;
85 int Opc = MI->getOpcode();
86 if (Opc == Hexagon::STriw_pred) {
87 // STriw_pred [R30], ofst, SrcReg;
88 unsigned FP = MI->getOperand(0).getReg();
Eric Christopherd9134482014-08-04 21:25:23 +000089 assert(
90 FP ==
91 QTM.getSubtargetImpl()->getRegisterInfo()->getFrameRegister() &&
92 "Not a Frame Pointer, Nor a Spill Slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +000093 assert(MI->getOperand(1).isImm() && "Not an offset");
94 int Offset = MI->getOperand(1).getImm();
95 int SrcReg = MI->getOperand(2).getReg();
96 assert(Hexagon::PredRegsRegClass.contains(SrcReg) &&
97 "Not a predicate register");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000098 if (!TII->isValidOffset(Hexagon::STriw_indexed, Offset)) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +000099 if (!TII->isValidOffset(Hexagon::ADD_ri, Offset)) {
100 BuildMI(*MBB, MII, MI->getDebugLoc(),
101 TII->get(Hexagon::CONST32_Int_Real),
102 HEXAGON_RESERVED_REG_1).addImm(Offset);
Colin LeMahieuefa74e02014-11-18 20:28:11 +0000103 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::A2_add),
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000104 HEXAGON_RESERVED_REG_1)
105 .addReg(FP).addReg(HEXAGON_RESERVED_REG_1);
106 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::TFR_RsPd),
107 HEXAGON_RESERVED_REG_2).addReg(SrcReg);
108 BuildMI(*MBB, MII, MI->getDebugLoc(),
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000109 TII->get(Hexagon::STriw_indexed))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000110 .addReg(HEXAGON_RESERVED_REG_1)
111 .addImm(0).addReg(HEXAGON_RESERVED_REG_2);
112 } else {
113 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::ADD_ri),
114 HEXAGON_RESERVED_REG_1).addReg(FP).addImm(Offset);
115 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::TFR_RsPd),
116 HEXAGON_RESERVED_REG_2).addReg(SrcReg);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000117 BuildMI(*MBB, MII, MI->getDebugLoc(),
118 TII->get(Hexagon::STriw_indexed))
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000119 .addReg(HEXAGON_RESERVED_REG_1)
120 .addImm(0)
121 .addReg(HEXAGON_RESERVED_REG_2);
122 }
123 } else {
124 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::TFR_RsPd),
125 HEXAGON_RESERVED_REG_2).addReg(SrcReg);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000126 BuildMI(*MBB, MII, MI->getDebugLoc(),
127 TII->get(Hexagon::STriw_indexed)).
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000128 addReg(FP).addImm(Offset).addReg(HEXAGON_RESERVED_REG_2);
129 }
130 MII = MBB->erase(MI);
131 --MII;
132 } else if (Opc == Hexagon::LDriw_pred) {
133 // DstReg = LDriw_pred [R30], ofst.
134 int DstReg = MI->getOperand(0).getReg();
135 assert(Hexagon::PredRegsRegClass.contains(DstReg) &&
136 "Not a predicate register");
137 unsigned FP = MI->getOperand(1).getReg();
Eric Christopherd9134482014-08-04 21:25:23 +0000138 assert(
139 FP ==
140 QTM.getSubtargetImpl()->getRegisterInfo()->getFrameRegister() &&
141 "Not a Frame Pointer, Nor a Spill Slot");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000142 assert(MI->getOperand(2).isImm() && "Not an offset");
143 int Offset = MI->getOperand(2).getImm();
144 if (!TII->isValidOffset(Hexagon::LDriw, Offset)) {
145 if (!TII->isValidOffset(Hexagon::ADD_ri, Offset)) {
146 BuildMI(*MBB, MII, MI->getDebugLoc(),
147 TII->get(Hexagon::CONST32_Int_Real),
148 HEXAGON_RESERVED_REG_1).addImm(Offset);
Colin LeMahieuefa74e02014-11-18 20:28:11 +0000149 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::A2_add),
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000150 HEXAGON_RESERVED_REG_1)
151 .addReg(FP)
152 .addReg(HEXAGON_RESERVED_REG_1);
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 } else {
160 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::ADD_ri),
161 HEXAGON_RESERVED_REG_1).addReg(FP).addImm(Offset);
162 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::LDriw),
163 HEXAGON_RESERVED_REG_2)
164 .addReg(HEXAGON_RESERVED_REG_1)
165 .addImm(0);
166 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::TFR_PdRs),
167 DstReg).addReg(HEXAGON_RESERVED_REG_2);
168 }
169 } else {
170 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::LDriw),
171 HEXAGON_RESERVED_REG_2).addReg(FP).addImm(Offset);
172 BuildMI(*MBB, MII, MI->getDebugLoc(), TII->get(Hexagon::TFR_PdRs),
173 DstReg).addReg(HEXAGON_RESERVED_REG_2);
174 }
175 MII = MBB->erase(MI);
176 --MII;
177 }
178 }
179 }
180
181 return true;
182}
183
184}
185
186//===----------------------------------------------------------------------===//
187// Public Constructor Functions
188//===----------------------------------------------------------------------===//
189
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +0000190static void initializePassOnce(PassRegistry &Registry) {
191 const char *Name = "Hexagon Expand Predicate Spill Code";
192 PassInfo *PI = new PassInfo(Name, "hexagon-spill-pred",
193 &HexagonExpandPredSpillCode::ID,
Craig Topper062a2ba2014-04-25 05:30:21 +0000194 nullptr, false, false);
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +0000195 Registry.registerPass(*PI, true);
196}
197
198void llvm::initializeHexagonExpandPredSpillCodePass(PassRegistry &Registry) {
199 CALL_ONCE_INITIALIZATION(initializePassOnce)
200}
201
Krzysztof Parzyszekd5007472013-05-06 18:38:37 +0000202FunctionPass*
203llvm::createHexagonExpandPredSpillCode(const HexagonTargetMachine &TM) {
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000204 return new HexagonExpandPredSpillCode(TM);
205}