1 //===-- ZPUFloatingPoint.cpp - Floating point Reg -> Stack converter ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
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"
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"
54 struct ZPUStackSlot
:public MachineFunctionPass
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;
83 llvm::createZPUStackSlotPass ()
85 return new ZPUStackSlot ();
88 // This function will replace registers with frameindex
89 bool ZPUStackSlot::assignStackSlots (MachineFunction
& Fn
, int *RegToStack
)
93 const TargetInstrInfo
*
94 TII
= Fn
.getTarget ().getInstrInfo ();
95 for (MachineFunction::iterator MFI
= Fn
.begin (), E
= Fn
.end (); MFI
!= E
;
102 for (MachineBasicBlock::iterator MBBI
= MBB
->begin (), EE
= MBB
->end ();
105 const MachineInstr
&MI
= *MBBI
;
106 DebugLoc dl
= MI
.getDebugLoc ();
110 printf("Testing \n");
112 for (unsigned i
= 0, e
= MI
.getNumOperands (); i
!= e
; ++i
)
114 const MachineOperand
&MO
= MI
.getOperand (i
);
118 unsigned Reg
= MO
.getReg ();
119 if (Reg
>= ZPU::R0
&& Reg
<= ZPU::R3
)
121 /* yes, we need to patch up this instruction! */
131 MachineInstr
& MI
= *MBBI
;
133 MachineInstrBuilder b
= BuildMI (*MBB
, MBBI
, dl
, TII
->get (MI
.getOpcode ()));
134 printf("Selected \n");
136 for (unsigned i
= 0, e
= MI
.getNumOperands (); i
!= e
; i
++)
138 const MachineOperand
&MO
= MI
.getOperand (i
);
142 unsigned Reg
= MO
.getReg ();
143 if (Reg
>= ZPU::R0
&& Reg
<= ZPU::R3
)
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
;
157 FrameIdx
= RegToStack
[Reg
];
160 b
.addFrameIndex (FrameIdx
);
164 ((MachineInstr
*) b
)->
165 addOperand (MI
.getOperand (i
));
170 ((MachineInstr
*) b
)->addOperand (MI
.getOperand (i
));
174 printf("Created \n");
175 ((MachineInstr
*) b
)->dump();
179 MI
.eraseFromParent ();
181 // Crappy way out getting the iterator right, slow for big bb.
182 MBBI
= MBB
->begin ();
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.
196 // [sp+0] = add [sp+0], [sp+1]
207 // STACKSTORE/STACKLOAD instructions should be removed since
209 // ZPUSTORSTACKSLOT fi#2,0,R0
214 ZPUStackSlot::insertStackInstructions (MachineFunction
& Fn
)
216 const TargetInstrInfo
*TII
= Fn
.getTarget ().getInstrInfo ();
217 for (MachineFunction::iterator MFI
= Fn
.begin (), E
= Fn
.end (); MFI
!= E
;
220 MachineBasicBlock
*MBB
= MFI
;
222 for (MachineBasicBlock::iterator MBBI
= MBB
->begin (), EE
= MBB
->end ();
226 * MBBI points to a basic block machine instruction
228 MachineInstr
& MI
= *MBBI
;
229 DebugLoc dl
= MI
.getDebugLoc ();
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
;
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
);
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
);
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();
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
);
299 if(MI
.getOpcode() == ZPU::ZPUSTORSTACKSLOT
||
300 MI
.getOpcode() == ZPU::ZPULOADSTACKSLOT
)
302 // xxxxSTACKSLOT have a constant, do something with this?
306 assert(0 && "Don't kow what to do with this");
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.
322 /// runOnMachineFunction - Loop over all of the basic blocks, transforming FP
323 /// register references into FP stack references.
325 bool ZPUStackSlot::runOnMachineFunction (MachineFunction
& Fn
)
329 // Reg to stack mapping, replace by proper map
333 memset (&RegToStack
[0], -1, sizeof (RegToStack
[0]) * 1024);
335 ret
= assignStackSlots (Fn
, RegToStack
);
336 insertStackInstructions (Fn
);