Don't generate carry bit when loading immediate values on the Microblaze.
[oota-llvm.git] / lib / Target / MBlaze / MBlazeFrameInfo.cpp
1 //=======- MBlazeFrameInfo.cpp - MBlaze Frame Information ------*- C++ -*-====//
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 file contains the MBlaze implementation of TargetFrameInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MBlazeFrameInfo.h"
15 #include "MBlazeInstrInfo.h"
16 #include "MBlazeMachineFunction.h"
17 #include "InstPrinter/MBlazeInstPrinter.h"
18 #include "llvm/Function.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineModuleInfo.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/Target/TargetData.h"
25 #include "llvm/Target/TargetOptions.h"
26 #include "llvm/Support/CommandLine.h"
27
28 using namespace llvm;
29
30 namespace llvm {
31   cl::opt<bool> DisableStackAdjust(
32     "disable-mblaze-stack-adjust",
33     cl::init(false),
34     cl::desc("Disable MBlaze stack layout adjustment."),
35     cl::Hidden);
36 }
37
38 //===----------------------------------------------------------------------===//
39 //
40 // Stack Frame Processing methods
41 // +----------------------------+
42 //
43 // The stack is allocated decrementing the stack pointer on
44 // the first instruction of a function prologue. Once decremented,
45 // all stack references are are done through a positive offset
46 // from the stack/frame pointer, so the stack is considered
47 // to grow up.
48 //
49 //===----------------------------------------------------------------------===//
50
51 static void analyzeFrameIndexes(MachineFunction &MF) {
52   if (DisableStackAdjust) return;
53
54   MachineFrameInfo *MFI = MF.getFrameInfo();
55   MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
56   const MachineRegisterInfo &MRI = MF.getRegInfo();
57
58   MachineRegisterInfo::livein_iterator LII = MRI.livein_begin();
59   MachineRegisterInfo::livein_iterator LIE = MRI.livein_end();
60   const SmallVector<int, 16> &LiveInFI = MBlazeFI->getLiveIn();
61   SmallVector<MachineInstr*, 16> EraseInstr;
62
63   MachineBasicBlock *MBB = MF.getBlockNumbered(0);
64   MachineBasicBlock::iterator MIB = MBB->begin();
65   MachineBasicBlock::iterator MIE = MBB->end();
66
67   int StackAdjust = 0;
68   int StackOffset = -28;
69   for (unsigned i = 0, e = LiveInFI.size(); i < e; ++i) {
70     for (MachineBasicBlock::iterator I=MIB; I != MIE; ++I) {
71       if (I->getOpcode() != MBlaze::LWI || I->getNumOperands() != 3 ||
72           !I->getOperand(1).isFI() || !I->getOperand(0).isReg() ||
73           I->getOperand(1).getIndex() != LiveInFI[i]) continue;
74
75       unsigned FIReg = I->getOperand(0).getReg();
76       MachineBasicBlock::iterator SI = I;
77       for (SI++; SI != MIE; ++SI) {
78         if (!SI->getOperand(0).isReg()) continue;
79         if (!SI->getOperand(1).isFI()) continue;
80         if (SI->getOpcode() != MBlaze::SWI) continue;
81
82         int FI = SI->getOperand(1).getIndex();
83         if (SI->getOperand(0).getReg() != FIReg) continue;
84         if (MFI->isFixedObjectIndex(FI)) continue;
85         if (MFI->getObjectSize(FI) != 4) continue;
86         if (SI->getOperand(0).isDef()) break;
87
88         if (SI->getOperand(0).isKill())
89           EraseInstr.push_back(I);
90         EraseInstr.push_back(SI);
91         MBlazeFI->recordLoadArgsFI(FI, StackOffset);
92         StackOffset -= 4;
93         StackAdjust += 4;
94         break;
95       }
96     }
97   }
98
99   for (MachineBasicBlock::iterator I=MBB->begin(), E=MBB->end(); I != E; ++I) {
100     if (I->getOpcode() != MBlaze::SWI || I->getNumOperands() != 3 ||
101         !I->getOperand(1).isFI() || !I->getOperand(0).isReg() ||
102         I->getOperand(1).getIndex() < 0) continue;
103
104     unsigned FIReg = 0;
105     for (MachineRegisterInfo::livein_iterator LI = LII; LI != LIE; ++LI) {
106       if (I->getOperand(0).getReg() == LI->first) {
107         FIReg = LI->first;
108         break;
109       }
110     }
111
112     if (FIReg) {
113       int FI = I->getOperand(1).getIndex();
114       MBlazeFI->recordLiveIn(FI);
115
116       StackAdjust += 4;
117       switch (FIReg) {
118       default: llvm_unreachable("invalid incoming parameter!");
119       case MBlaze::R5:  MBlazeFI->recordLoadArgsFI(FI, -4); break;
120       case MBlaze::R6:  MBlazeFI->recordLoadArgsFI(FI, -8); break;
121       case MBlaze::R7:  MBlazeFI->recordLoadArgsFI(FI, -12); break;
122       case MBlaze::R8:  MBlazeFI->recordLoadArgsFI(FI, -16); break;
123       case MBlaze::R9:  MBlazeFI->recordLoadArgsFI(FI, -20); break;
124       case MBlaze::R10: MBlazeFI->recordLoadArgsFI(FI, -24); break;
125       }
126     }
127   }
128
129   for (int i = 0, e = EraseInstr.size(); i < e; ++i)
130     MBB->erase(EraseInstr[i]);
131
132   MBlazeFI->setStackAdjust(StackAdjust);
133 }
134
135 static void interruptFrameLayout(MachineFunction &MF) {
136   const Function *F = MF.getFunction();
137   llvm::CallingConv::ID CallConv = F->getCallingConv();
138
139   // If this function is not using either the interrupt_handler
140   // calling convention or the save_volatiles calling convention
141   // then we don't need to do any additional frame layout.
142   if (CallConv != llvm::CallingConv::MBLAZE_INTR &&
143       CallConv != llvm::CallingConv::MBLAZE_SVOL)
144       return;
145
146   MachineFrameInfo *MFI = MF.getFrameInfo();
147   const MachineRegisterInfo &MRI = MF.getRegInfo();
148   const MBlazeInstrInfo &TII =
149     *static_cast<const MBlazeInstrInfo*>(MF.getTarget().getInstrInfo());
150
151   // Determine if the calling convention is the interrupt_handler
152   // calling convention. Some pieces of the prologue and epilogue
153   // only need to be emitted if we are lowering and interrupt handler.
154   bool isIntr = CallConv == llvm::CallingConv::MBLAZE_INTR;
155
156   // Determine where to put prologue and epilogue additions
157   MachineBasicBlock &MENT   = MF.front();
158   MachineBasicBlock &MEXT   = MF.back();
159
160   MachineBasicBlock::iterator MENTI = MENT.begin();
161   MachineBasicBlock::iterator MEXTI = prior(MEXT.end());
162
163   DebugLoc ENTDL = MENTI != MENT.end() ? MENTI->getDebugLoc() : DebugLoc();
164   DebugLoc EXTDL = MEXTI != MEXT.end() ? MEXTI->getDebugLoc() : DebugLoc();
165
166   // Store the frame indexes generated during prologue additions for use
167   // when we are generating the epilogue additions.
168   SmallVector<int, 10> VFI;
169
170   // Build the prologue SWI for R3 - R12 if needed. Note that R11 must
171   // always have a SWI because it is used when processing RMSR.
172   for (unsigned r = MBlaze::R3; r <= MBlaze::R12; ++r) {
173     if (!MRI.isPhysRegUsed(r) && !(isIntr && r == MBlaze::R11)) continue;
174     
175     int FI = MFI->CreateStackObject(4,4,false,false);
176     VFI.push_back(FI);
177
178     BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), r)
179       .addFrameIndex(FI).addImm(0);
180   }
181     
182   // Build the prologue SWI for R17, R18
183   int R17FI = MFI->CreateStackObject(4,4,false,false);
184   int R18FI = MFI->CreateStackObject(4,4,false,false);
185
186   BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), MBlaze::R17)
187     .addFrameIndex(R17FI).addImm(0);
188     
189   BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), MBlaze::R18)
190     .addFrameIndex(R18FI).addImm(0);
191
192   // Buid the prologue SWI and the epilogue LWI for RMSR if needed
193   if (isIntr) {
194     int MSRFI = MFI->CreateStackObject(4,4,false,false);
195     BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::MFS), MBlaze::R11)
196       .addReg(MBlaze::RMSR);
197     BuildMI(MENT, MENTI, ENTDL, TII.get(MBlaze::SWI), MBlaze::R11)
198       .addFrameIndex(MSRFI).addImm(0);
199
200     BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), MBlaze::R11)
201       .addFrameIndex(MSRFI).addImm(0);
202     BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::MTS), MBlaze::RMSR)
203       .addReg(MBlaze::R11);
204   }
205
206   // Build the epilogue LWI for R17, R18
207   BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), MBlaze::R18)
208     .addFrameIndex(R18FI).addImm(0);
209
210   BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), MBlaze::R17)
211     .addFrameIndex(R17FI).addImm(0);
212
213   // Build the epilogue LWI for R3 - R12 if needed
214   for (unsigned r = MBlaze::R12, i = VFI.size(); r >= MBlaze::R3; --r) {
215     if (!MRI.isPhysRegUsed(r)) continue;
216     BuildMI(MEXT, MEXTI, EXTDL, TII.get(MBlaze::LWI), r)
217       .addFrameIndex(VFI[--i]).addImm(0);
218   }
219 }
220
221 static void determineFrameLayout(MachineFunction &MF) {
222   MachineFrameInfo *MFI = MF.getFrameInfo();
223   MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
224
225   // Replace the dummy '0' SPOffset by the negative offsets, as explained on
226   // LowerFORMAL_ARGUMENTS. Leaving '0' for while is necessary to avoid
227   // the approach done by calculateFrameObjectOffsets to the stack frame.
228   MBlazeFI->adjustLoadArgsFI(MFI);
229   MBlazeFI->adjustStoreVarArgsFI(MFI);
230
231   // Get the number of bytes to allocate from the FrameInfo
232   unsigned FrameSize = MFI->getStackSize();
233   FrameSize -= MBlazeFI->getStackAdjust();
234
235   // Get the alignments provided by the target, and the maximum alignment
236   // (if any) of the fixed frame objects.
237   // unsigned MaxAlign = MFI->getMaxAlignment();
238   unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
239   unsigned AlignMask = TargetAlign - 1;
240
241   // Make sure the frame is aligned.
242   FrameSize = (FrameSize + AlignMask) & ~AlignMask;
243   MFI->setStackSize(FrameSize);
244 }
245
246 // hasFP - Return true if the specified function should have a dedicated frame
247 // pointer register.  This is true if the function has variable sized allocas or
248 // if frame pointer elimination is disabled.
249 bool MBlazeFrameInfo::hasFP(const MachineFunction &MF) const {
250   const MachineFrameInfo *MFI = MF.getFrameInfo();
251   return DisableFramePointerElim(MF) || MFI->hasVarSizedObjects();
252 }
253
254 void MBlazeFrameInfo::emitPrologue(MachineFunction &MF) const {
255   MachineBasicBlock &MBB   = MF.front();
256   MachineFrameInfo *MFI    = MF.getFrameInfo();
257   const MBlazeInstrInfo &TII =
258     *static_cast<const MBlazeInstrInfo*>(MF.getTarget().getInstrInfo());
259   MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
260   MachineBasicBlock::iterator MBBI = MBB.begin();
261   DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
262
263   llvm::CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
264   bool requiresRA = CallConv == llvm::CallingConv::MBLAZE_INTR;
265
266   // Determine the correct frame layout
267   determineFrameLayout(MF);
268
269   // Get the number of bytes to allocate from the FrameInfo.
270   unsigned StackSize = MFI->getStackSize();
271
272   // No need to allocate space on the stack.
273   if (StackSize == 0 && !MFI->adjustsStack() && !requiresRA) return;
274
275   int FPOffset = MBlazeFI->getFPStackOffset();
276   int RAOffset = MBlazeFI->getRAStackOffset();
277
278   // Adjust stack : addi R1, R1, -imm
279   BuildMI(MBB, MBBI, DL, TII.get(MBlaze::ADDIK), MBlaze::R1)
280       .addReg(MBlaze::R1).addImm(-StackSize);
281
282   // swi  R15, R1, stack_loc
283   if (MFI->adjustsStack() || requiresRA) {
284     BuildMI(MBB, MBBI, DL, TII.get(MBlaze::SWI))
285         .addReg(MBlaze::R15).addReg(MBlaze::R1).addImm(RAOffset);
286   }
287
288   if (hasFP(MF)) {
289     // swi  R19, R1, stack_loc
290     BuildMI(MBB, MBBI, DL, TII.get(MBlaze::SWI))
291       .addReg(MBlaze::R19).addReg(MBlaze::R1).addImm(FPOffset);
292
293     // add R19, R1, R0
294     BuildMI(MBB, MBBI, DL, TII.get(MBlaze::ADD), MBlaze::R19)
295       .addReg(MBlaze::R1).addReg(MBlaze::R0);
296   }
297 }
298
299 void MBlazeFrameInfo::emitEpilogue(MachineFunction &MF,
300                                    MachineBasicBlock &MBB) const {
301   MachineBasicBlock::iterator MBBI = prior(MBB.end());
302   MachineFrameInfo *MFI            = MF.getFrameInfo();
303   MBlazeFunctionInfo *MBlazeFI     = MF.getInfo<MBlazeFunctionInfo>();
304   const MBlazeInstrInfo &TII =
305     *static_cast<const MBlazeInstrInfo*>(MF.getTarget().getInstrInfo());
306
307   DebugLoc dl = MBBI->getDebugLoc();
308
309   llvm::CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
310   bool requiresRA = CallConv == llvm::CallingConv::MBLAZE_INTR;
311
312   // Get the FI's where RA and FP are saved.
313   int FPOffset = MBlazeFI->getFPStackOffset();
314   int RAOffset = MBlazeFI->getRAStackOffset();
315
316   if (hasFP(MF)) {
317     // add R1, R19, R0
318     BuildMI(MBB, MBBI, dl, TII.get(MBlaze::ADD), MBlaze::R1)
319       .addReg(MBlaze::R19).addReg(MBlaze::R0);
320
321     // lwi  R19, R1, stack_loc
322     BuildMI(MBB, MBBI, dl, TII.get(MBlaze::LWI), MBlaze::R19)
323       .addReg(MBlaze::R1).addImm(FPOffset);
324   }
325
326   // lwi R15, R1, stack_loc
327   if (MFI->adjustsStack() || requiresRA) {
328     BuildMI(MBB, MBBI, dl, TII.get(MBlaze::LWI), MBlaze::R15)
329       .addReg(MBlaze::R1).addImm(RAOffset);
330   }
331
332   // Get the number of bytes from FrameInfo
333   int StackSize = (int) MFI->getStackSize();
334
335   // addi R1, R1, imm
336   if (StackSize) {
337     BuildMI(MBB, MBBI, dl, TII.get(MBlaze::ADDIK), MBlaze::R1)
338       .addReg(MBlaze::R1).addImm(StackSize);
339   }
340 }
341
342 void MBlazeFrameInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,                                                            RegScavenger *RS)
343                                                            const {
344   MachineFrameInfo *MFI = MF.getFrameInfo();
345   MBlazeFunctionInfo *MBlazeFI = MF.getInfo<MBlazeFunctionInfo>();
346   llvm::CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
347   bool requiresRA = CallConv == llvm::CallingConv::MBLAZE_INTR;
348
349   if (MFI->adjustsStack() || requiresRA) {
350     MBlazeFI->setRAStackOffset(0);
351     MFI->CreateFixedObject(4,0,true);
352   }
353
354   if (hasFP(MF)) {
355     MBlazeFI->setFPStackOffset(4);
356     MFI->CreateFixedObject(4,4,true);
357   }
358
359   interruptFrameLayout(MF);
360   analyzeFrameIndexes(MF);
361 }