Fixed some bugs.
[llvm/zpu.git] / lib / Target / ZPU / ZPUStackSlot.cpp
blob927317f1c8627e5374f1c9ea1c0602926f55aaaf
1 //===-- ZPUFloatingPoint.cpp - Floating point Reg -> Stack converter ------===//
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 defines the pass which converts floating point instructions from
11 // pseudo registers into register stack instructions. This pass uses live
12 // variable information to indicate where the FPn registers are used and their
13 // lifetimes.
15 // The x87 hardware tracks liveness of the stack registers, so it is necessary
16 // to implement exact liveness tracking between basic blocks. The CFG edges are
17 // partitioned into bundles where the same FP registers must be live in
18 // identical stack positions. Instructions are inserted at the end of each basic
19 // block to rearrange the live registers to match the outgoing bundle.
21 // This approach avoids splitting critical edges at the potential cost of more
22 // live register shuffling instructions when critical edges are present.
24 //===----------------------------------------------------------------------===//
26 #define DEBUG_TYPE "ZPU-codegen"
27 #include "ZPU.h"
28 #include "ZPUInstrInfo.h"
29 #include "llvm/ADT/DepthFirstIterator.h"
30 #include "llvm/ADT/DenseMap.h"
31 #include "llvm/ADT/SmallPtrSet.h"
32 #include "llvm/ADT/SmallVector.h"
33 #include "llvm/ADT/Statistic.h"
34 #include "llvm/ADT/STLExtras.h"
35 #include "llvm/CodeGen/MachineFunctionPass.h"
36 #include "llvm/CodeGen/MachineInstrBuilder.h"
37 #include "llvm/CodeGen/MachineRegisterInfo.h"
38 #include "llvm/CodeGen/MachineFrameInfo.h"
39 #include "llvm/CodeGen/MachineOperand.h"
40 #include "llvm/CodeGen/Passes.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/ErrorHandling.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/Target/TargetInstrInfo.h"
45 #include "llvm/Target/TargetMachine.h"
46 #include <algorithm>
47 #include <stdio.h>
49 using namespace llvm;
52 namespace
54 struct ZPUStackSlot:public MachineFunctionPass
56 static char ID;
57 ZPUStackSlot ():MachineFunctionPass (ID)
61 virtual void getAnalysisUsage (AnalysisUsage & AU) const
63 AU.setPreservesCFG ();
64 AU.addPreservedID (MachineLoopInfoID);
65 AU.addPreservedID (MachineDominatorsID);
66 MachineFunctionPass::getAnalysisUsage (AU);
69 virtual bool runOnMachineFunction (MachineFunction & MF);
70 virtual bool assignStackSlots (MachineFunction & Fn, int *RegToStack);
71 virtual void insertStackInstructions (MachineFunction & Fn);
73 virtual const char *getPassName () const
75 return "ZPU Stackslotifier";
79 char ZPUStackSlot::ID = 0;
82 FunctionPass *
83 llvm::createZPUStackSlotPass ()
85 return new ZPUStackSlot ();
88 // This function will replace registers with frameindex
89 bool ZPUStackSlot::assignStackSlots (MachineFunction & Fn, int *RegToStack)
91 bool
92 foundmore = false;
93 const TargetInstrInfo *
94 TII = Fn.getTarget ().getInstrInfo ();
95 for (MachineFunction::iterator MFI = Fn.begin (), E = Fn.end (); MFI != E;
96 ++MFI)
98 MachineBasicBlock *
99 MBB = MFI;
102 for (MachineBasicBlock::iterator MBBI = MBB->begin (), EE = MBB->end ();
103 MBBI != EE; ++MBBI)
105 const MachineInstr &MI = *MBBI;
106 DebugLoc dl = MI.getDebugLoc ();
108 //MBBI->dump();
109 bool found = false;
110 printf("Testing \n");
111 MI.dump();
112 for (unsigned i = 0, e = MI.getNumOperands (); i != e; ++i)
114 const MachineOperand &MO = MI.getOperand (i);
116 if (MO.isReg ())
118 unsigned Reg = MO.getReg ();
119 if (Reg >= ZPU::R0 && Reg <= ZPU::R3)
121 /* yes, we need to patch up this instruction! */
122 found = true;
123 break;
128 if (found)
130 foundmore = true;
131 MachineInstr & MI = *MBBI;
133 MachineInstrBuilder b = BuildMI (*MBB, MBBI, dl, TII->get (MI.getOpcode ()));
134 printf("Selected \n");
135 MI.dump();
136 for (unsigned i = 0, e = MI.getNumOperands (); i != e; i++)
138 const MachineOperand &MO = MI.getOperand (i);
140 if (MO.isReg ())
142 unsigned Reg = MO.getReg ();
143 if (Reg >= ZPU::R0 && Reg <= ZPU::R3)
145 int FrameIdx;
146 MachineFrameInfo *MFI = Fn.getFrameInfo ();
148 // Determine if a new stack slot is required
149 if (RegToStack[Reg] == -1)
151 FrameIdx = MFI->CreateStackObject (4, 4, true);
153 RegToStack[Reg] = FrameIdx;
155 else
157 FrameIdx = RegToStack[Reg];
160 b.addFrameIndex (FrameIdx);
162 else
164 ((MachineInstr *) b)->
165 addOperand (MI.getOperand (i));
168 else
170 ((MachineInstr *) b)->addOperand (MI.getOperand (i));
174 printf("Created \n");
175 ((MachineInstr *) b)->dump();
176 printf("remove \n");
177 MI.dump();
179 MI.eraseFromParent ();
181 // Crappy way out getting the iterator right, slow for big bb.
182 MBBI = MBB->begin ();
186 printf("\n");
187 return foundmore;
190 // This function will insert loadsp/storesp instructions.
191 // For each RHS argument an loadsp will be inserted.
192 // For each LHS argument an storesp will be inserted.
194 // [sp+0] = im ...
195 // [sp+1] = im ...
196 // [sp+0] = add [sp+0], [sp+1]
198 // im
199 // storesp 0
200 // im
201 // storesp 1
202 // loadsp 1
203 // loadsp 1
204 // add
205 // storesp 1
207 // STACKSTORE/STACKLOAD instructions should be removed since
209 // ZPUSTORSTACKSLOT fi#2,0,R0
210 // is
211 // loadsp [R0]
212 // storesp fi#2
213 void
214 ZPUStackSlot::insertStackInstructions (MachineFunction & Fn)
216 const TargetInstrInfo *TII = Fn.getTarget ().getInstrInfo ();
217 for (MachineFunction::iterator MFI = Fn.begin (), E = Fn.end (); MFI != E;
218 ++MFI)
220 MachineBasicBlock *MBB = MFI;
222 for (MachineBasicBlock::iterator MBBI = MBB->begin (), EE = MBB->end ();
223 MBBI != EE; ++MBBI)
226 * MBBI points to a basic block machine instruction
228 MachineInstr & MI = *MBBI;
229 DebugLoc dl = MI.getDebugLoc ();
231 //MBBI->dump();
232 //MBB->dump();
234 int loadSPIndex = 0;
235 MI.dump();
237 // Loop backwards and start to with loadsp
238 for (int i = MI.getNumOperands () - 1; i >= 0; i--)
240 const MachineOperand & MO = MI.getOperand (i);
242 // Operand zero is LHS and should be a storesp
243 MachineInstr *tempInstr;
244 if (i == 0)
246 MachineInstr *tempInstr = NULL;
248 if (MO.getType () == MachineOperand::MO_FrameIndex)
250 tempInstr = MBB->getParent ()-> CreateMachineInstr (TII->get (ZPU::ZPUPSEUDOSTORESP),dl);
252 MachineInstrBuilder (tempInstr).addImm ( (MO.getIndex () + 1)*4);
254 else if(MO.getType() == MachineOperand::MO_GlobalAddress)
256 tempInstr = MBB->getParent ()->CreateMachineInstr (TII->get (ZPU::ZPUIM),dl);
258 MachineInstrBuilder (tempInstr).addOperand (MO);
260 if( MI.getOpcode() == ZPU::ZPUSTOREREG)
262 // Store operations needs to have its destination pushed on stack
263 // Put before instruction
264 MBB->insert(MBBI, tempInstr);
266 else
268 assert(tempInstr->getOpcode() == ZPU::ZPUPSEUDOSTORESP && "The result of the instruction should be stored to stack. With a storesp");
269 // Add the storesp after the instruction
270 MBB->insertAfter (MBBI, tempInstr);
271 MBBI++;
274 if(MI.getOpcode() == ZPU::ZPUSTORSTACKSLOT ||
275 MI.getOpcode() == ZPU::ZPULOADSTACKSLOT)
277 // Remove instruction if stackstore since this is same as storesp
278 MI.removeFromParent();
281 else
283 MachineInstr *tempInstr = NULL;
285 printf("Operand type is %d, %d \n",MO.getType(),i);
286 if (MO.getType () == MachineOperand::MO_FrameIndex)
288 tempInstr = MBB->getParent ()->CreateMachineInstr (TII->get (ZPU::ZPUPSEUDOLOADSP),dl);
289 MachineInstrBuilder (tempInstr).addImm ( (MO.getIndex () + loadSPIndex)*4 );
291 else if(MO.getType() == MachineOperand::MO_GlobalAddress)
293 tempInstr = MBB->getParent ()->CreateMachineInstr (TII->get (ZPU::ZPUIM),dl);
295 MachineInstrBuilder (tempInstr).addOperand (MO);
297 else
299 if(MI.getOpcode() == ZPU::ZPUSTORSTACKSLOT ||
300 MI.getOpcode() == ZPU::ZPULOADSTACKSLOT)
302 // xxxxSTACKSLOT have a constant, do something with this?
304 else
306 assert(0 && "Don't kow what to do with this");
309 if(tempInstr!=NULL)
311 MBB->insert (MBBI, tempInstr);
313 // The second loadsp should have its SP increased by one since the first loadsp
314 // takes one stack slot.
315 loadSPIndex++;
322 /// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
323 /// register references into FP stack references.
325 bool ZPUStackSlot::runOnMachineFunction (MachineFunction & Fn)
327 bool
328 ret;
329 // Reg to stack mapping, replace by proper map
331 RegToStack[1024];
333 memset (&RegToStack[0], -1, sizeof (RegToStack[0]) * 1024);
335 ret = assignStackSlots (Fn, RegToStack);
336 insertStackInstructions (Fn);
338 return ret;