33 #include <llvm/IR/Function.h>
34 #include <llvm/CodeGen/MachineFrameInfo.h>
35 #include <llvm/CodeGen/MachineInstrBuilder.h>
51 #define ERASE_INSTR_AND_RETURN(I) return MBB.erase(I)
54 #define ADDIMM TCE::ADD64ssa
55 #define SUBIMM TCE::SUB64ssa
56 #define STREG TCE::ST64ss
57 #define LDREG TCE::LD64ss
58 #define LDRA TCE::LD64RAs
59 #define STRA TCE::ST64RAss
60 #define MOVREG TCE::MOV64ss
62 #define ADDIMM TCE::ADDrri
63 #define SUBIMM TCE::SUBrri
64 #define MOVREG TCE::MOVI32rr
65 #ifdef LITTLE_ENDIAN_TARGET
66 #define STREG TCE::ST32rr
67 #define LDREG TCE::LD32rr
68 #define LDRA TCE::LD32RAr
69 #define STRA TCE::ST32RArr
71 #define STREG TCE::STWrr
72 #define LDREG TCE::LDWrr
73 #define LDRA TCE::LDWRAr
74 #define STRA TCE::STWRArr
84 MachineBasicBlock::iterator
86 MachineFunction &MF, MachineBasicBlock &MBB,
87 MachineBasicBlock::iterator I)
const {
89 int opc = I->getOpcode();
91 if (opc == TCE::ADJCALLSTACKDOWN) {
92 MachineOperand mo1 = I->getOperand(2);
93 MachineOperand mo2 = I->getOperand(3);
94 long val = I->getOperand(0).getImm();
100 I->setDesc(
tii_.get(std::get<0>(spOpcAndOffset)));
101 I->getOperand(0).ChangeToRegister(mo1.getReg(), mo1.isDef(),
105 I->getOperand(1).ChangeToRegister(mo2.getReg(),
false,
false, mo2.isKill(),
106 false,
false, mo2.isDebug());
107 I->getOperand(2).ChangeToImmediate(std::get<1>(spOpcAndOffset));
110 }
else if (opc == TCE::ADJCALLSTACKUP) {
111 MachineOperand mo1 = I->getOperand(2);
112 MachineOperand mo2 = I->getOperand(3);
113 long val = I->getOperand(0).getImm();
118 I->getOperand(0).ChangeToRegister(mo1.getReg(), mo1.isDef(),
122 I->getOperand(1).ChangeToRegister(mo2.getReg(),
false,
false, mo2.isKill(),
123 false,
false, mo2.isDebug());
124 I->getOperand(2).ChangeToImmediate(val);
125 #ifdef LLVM_OLDER_THAN_15
136 #undef ERASE_INSTR_AND_RETURN
139 if (MF.getFrameInfo().hasVarSizedObjects()) {
147 for (MachineFunction::const_iterator i = mf.begin(); i != mf.end(); i++) {
148 const MachineBasicBlock& mbb = *i;
149 for (MachineBasicBlock::const_iterator j = mbb.begin();
150 j != mbb.end(); j++){
151 const MachineInstr& ins = *j;
153 ins.getOpcode() == TCE::CALL_MEMrr ||
154 ins.getOpcode() == TCE::CALL_MEMri) {
168 MachineBasicBlock& mbb = mf.front();
169 MachineFrameInfo& mfi = mf.getFrameInfo();
170 int numBytes = (int)mfi.getStackSize();
173 bool hasCalls = mfi.hasCalls();
183 int varBytes = numBytes;
185 MachineBasicBlock::iterator ii = mbb.begin();
187 DebugLoc dl = (ii != mbb.end() ?
188 ii->getDebugLoc() : DebugLoc());
195 BuildMI(mbb, ii, dl,
tii_.get(std::get<0>(spOpcAndOffset)), TCE::SP)
197 .addImm(std::get<1>(spOpcAndOffset));
200 BuildMI(mbb, ii, dl,
tii_.get(
STRA))
204 .setMIFlag(MachineInstr::FrameSetup);
208 MachineBasicBlock::iterator raStore = ii; raStore--;
209 LLVMContext& context = mbb.getParent()->getFunction().getContext();
211 llvm::MDString::get(context,
"AA_CATEGORY_RA_SAVE_SLOT");
213 MDNode::get(context, llvm::ArrayRef<llvm::Metadata*>(&md, 1));
215 MachineOperand metaDataOperand = MachineOperand::CreateMetadata(mdNode);
216 raStore->addOperand(metaDataOperand);
221 if (!mf.getFunction().doesNotReturn()) {
223 BuildMI(mbb, ii, dl,
tii_.get(std::get<0>(spOpcAndOffset)),
226 .addImm(std::get<1>(spOpcAndOffset));
232 .setMIFlag(MachineInstr::FrameSetup);
236 MachineBasicBlock::iterator fpStore = ii; fpStore--;
237 LLVMContext& context = mbb.getParent()->getFunction().getContext();
239 llvm::MDString::get(context,
"AA_CATEGORY_FP_SAVE_SLOT");
241 MDNode::get(context, llvm::ArrayRef<llvm::Metadata*>(&md, 1));
243 MachineOperand metaDataOperand = MachineOperand::CreateMetadata(mdNode);
244 fpStore->addOperand(metaDataOperand);
247 BuildMI(mbb, ii, dl,
tii_.get(
MOVREG), TCE::FP).addReg(TCE::SP)
248 .setMIFlag(MachineInstr::FrameSetup);
251 mfi.setStackSize(numBytes);
256 BuildMI(mbb, ii, dl,
tii_.get(std::get<0>(spOpcAndOffset)), TCE::SP)
258 .addImm(std::get<1>(spOpcAndOffset));
267 MachineFunction& mf, MachineBasicBlock& mbb)
const {
269 MachineFrameInfo& mfi = mf.getFrameInfo();
271 MachineBasicBlock::iterator mbbi = std::prev(mbb.end());
273 DebugLoc dl = mbbi->getDebugLoc();
275 if (mbbi->getOpcode() != TCE::RETL) {
276 assert(
false &&
"ERROR: Inserting epilogue w/o return?");
279 unsigned numBytes = mfi.getStackSize();
281 unsigned varBytes = numBytes;
288 bool hasCalls = mfi.hasCalls();
299 BuildMI(mbb, mbbi, dl,
tii_.get(
MOVREG), TCE::SP)
301 .setMIFlag(MachineInstr::FrameSetup);
304 BuildMI(mbb, mbbi, dl,
tii_.get(
LDREG), TCE::FP)
307 .setMIFlag(MachineInstr::FrameSetup);
310 MachineBasicBlock::iterator fpLoad = mbbi; fpLoad--;
312 LLVMContext& context =
313 mbb.getParent()->getFunction().getContext();
315 llvm::Metadata* md = llvm::MDString::get(context,
"AA_CATEGORY_FP_SAVE_SLOT");
316 MDNode* mdNode = MDNode::get(context, llvm::ArrayRef<llvm::Metadata*>(&md, 1));
318 MachineOperand metaDataOperand = MachineOperand::CreateMetadata(mdNode);
319 fpLoad->addOperand(metaDataOperand);
322 BuildMI(mbb, mbbi, dl,
tii_.get(
ADDIMM), TCE::SP)
328 BuildMI(mbb, mbbi, dl,
tii_.get(
ADDIMM), TCE::SP)
339 .setMIFlag(MachineInstr::FrameSetup);
341 MachineBasicBlock::iterator raLoad = mbbi; raLoad--;
343 LLVMContext& context =
344 mbb.getParent()->getFunction().getContext();
346 llvm::Metadata* md = llvm::MDString::get(context,
"AA_CATEGORY_RA_SAVE_SLOT");
347 MDNode* mdNode = MDNode::get(context, llvm::ArrayRef<llvm::Metadata*>(&md, 1));
349 MachineOperand metaDataOperand = MachineOperand::CreateMetadata(mdNode);
350 raLoad->addOperand(metaDataOperand);
353 BuildMI(mbb, mbbi, dl,
tii_.get(
ADDIMM), TCE::SP)