llvm.org GIT mirror llvm / 72ad17c
[Sparc] Generate correct code for leaf functions with stack objects git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@183067 91177308-0d34-0410-b5e6-96231b3b80d8 Venkatraman Govindaraju 6 years ago
5 changed file(s) with 103 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
3434
3535 void SparcFrameLowering::emitPrologue(MachineFunction &MF) const {
3636 SparcMachineFunctionInfo *FuncInfo = MF.getInfo();
37 if (FuncInfo->isLeafProc())
38 return;
3937
4038 MachineBasicBlock &MBB = MF.front();
4139 MachineFrameInfo *MFI = MF.getFrameInfo();
4745 // Get the number of bytes to allocate from the FrameInfo
4846 int NumBytes = (int) MFI->getStackSize();
4947
50 if (SubTarget.is64Bit()) {
51 // All 64-bit stack frames must be 16-byte aligned, and must reserve space
52 // for spilling the 16 window registers at %sp+BIAS..%sp+BIAS+128.
53 NumBytes += 128;
54 // Frames with calls must also reserve space for 6 outgoing arguments
55 // whether they are used or not. LowerCall_64 takes care of that.
56 assert(NumBytes % 16 == 0 && "Stack size not 16-byte aligned");
57 } else {
58 // Emit the correct save instruction based on the number of bytes in
59 // the frame. Minimum stack frame size according to V8 ABI is:
60 // 16 words for register window spill
61 // 1 word for address of returned aggregate-value
62 // + 6 words for passing parameters on the stack
63 // ----------
64 // 23 words * 4 bytes per word = 92 bytes
65 NumBytes += 92;
66
67 // Round up to next doubleword boundary -- a double-word boundary
68 // is required by the ABI.
69 NumBytes = RoundUpToAlignment(NumBytes, 8);
70 }
71 NumBytes = -NumBytes;
48 unsigned SAVEri = SP::SAVEri;
49 unsigned SAVErr = SP::SAVErr;
50 if (FuncInfo->isLeafProc()) {
51 if (NumBytes == 0)
52 return;
53 SAVEri = SP::ADDri;
54 SAVErr = SP::ADDrr;
55 }
56 NumBytes = - SubTarget.getAdjustedFrameSize(NumBytes);
7257
7358 if (NumBytes >= -4096) {
74 BuildMI(MBB, MBBI, dl, TII.get(SP::SAVEri), SP::O6)
59 BuildMI(MBB, MBBI, dl, TII.get(SAVEri), SP::O6)
7560 .addReg(SP::O6).addImm(NumBytes);
7661 } else {
7762 // Emit this the hard way. This clobbers G1 which we always know is
8166 // Emit G1 = G1 + I6
8267 BuildMI(MBB, MBBI, dl, TII.get(SP::ORri), SP::G1)
8368 .addReg(SP::G1).addImm(NumBytes & ((1 << 10)-1));
84 BuildMI(MBB, MBBI, dl, TII.get(SP::SAVErr), SP::O6)
69 BuildMI(MBB, MBBI, dl, TII.get(SAVErr), SP::O6)
8570 .addReg(SP::O6).addReg(SP::G1);
8671 }
8772 }
10893 void SparcFrameLowering::emitEpilogue(MachineFunction &MF,
10994 MachineBasicBlock &MBB) const {
11095 SparcMachineFunctionInfo *FuncInfo = MF.getInfo();
111 if (FuncInfo->isLeafProc())
112 return;
11396 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
11497 const SparcInstrInfo &TII =
11598 *static_cast(MF.getTarget().getInstrInfo());
11699 DebugLoc dl = MBBI->getDebugLoc();
117100 assert(MBBI->getOpcode() == SP::RETL &&
118101 "Can only put epilog before 'retl' instruction!");
119 BuildMI(MBB, MBBI, dl, TII.get(SP::RESTORErr), SP::G0).addReg(SP::G0)
120 .addReg(SP::G0);
102 if (!FuncInfo->isLeafProc()) {
103 BuildMI(MBB, MBBI, dl, TII.get(SP::RESTORErr), SP::G0).addReg(SP::G0)
104 .addReg(SP::G0);
105 return;
106 }
107 MachineFrameInfo *MFI = MF.getFrameInfo();
108
109 int NumBytes = (int) MFI->getStackSize();
110 if (NumBytes == 0)
111 return;
112
113 NumBytes = SubTarget.getAdjustedFrameSize(NumBytes);
114
115 if (NumBytes < 4096) {
116 BuildMI(MBB, MBBI, dl, TII.get(SP::ADDri), SP::O6)
117 .addReg(SP::O6).addImm(NumBytes);
118 } else {
119 // Emit this the hard way. This clobbers G1 which we always know is
120 // available here.
121 unsigned OffHi = (unsigned)NumBytes >> 10U;
122 BuildMI(MBB, MBBI, dl, TII.get(SP::SETHIi), SP::G1).addImm(OffHi);
123 // Emit G1 = G1 + I6
124 BuildMI(MBB, MBBI, dl, TII.get(SP::ORri), SP::G1)
125 .addReg(SP::G1).addImm(NumBytes & ((1 << 10)-1));
126 BuildMI(MBB, MBBI, dl, TII.get(SP::ADDrr), SP::O6)
127 .addReg(SP::O6).addReg(SP::G1);
128 }
121129 }
122130
123131 bool SparcFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
9090 MI.getOperand(FIOperandNum + 1).getImm() +
9191 Subtarget.getStackPointerBias();
9292 SparcMachineFunctionInfo *FuncInfo = MF.getInfo();
93 unsigned FramePtr = (FuncInfo->isLeafProc()) ? SP::O6 : SP::I6;
93 unsigned FramePtr = SP::I6;
94 if (FuncInfo->isLeafProc()) {
95 //Use %sp and adjust offset if needed.
96 FramePtr = SP::O6;
97 int stackSize = MF.getFrameInfo()->getStackSize();
98 Offset += (stackSize) ? Subtarget.getAdjustedFrameSize(stackSize) : 0 ;
99 }
100
94101 // Replace frame index with a frame pointer reference.
95102 if (Offset >= -4096 && Offset <= 4095) {
96103 // If the offset is small enough to fit in the immediate field, directly
1212
1313 #include "SparcSubtarget.h"
1414 #include "Sparc.h"
15 #include "llvm/Support/MathExtras.h"
1516 #include "llvm/Support/TargetRegistry.h"
1617
1718 #define GET_SUBTARGETINFO_TARGET_DESC
4344 // Parse features string.
4445 ParseSubtargetFeatures(CPUName, FS);
4546 }
47
48
49 int SparcSubtarget::getAdjustedFrameSize(int frameSize) const {
50
51 if (is64Bit()) {
52 // All 64-bit stack frames must be 16-byte aligned, and must reserve space
53 // for spilling the 16 window registers at %sp+BIAS..%sp+BIAS+128.
54 frameSize += 128;
55 // Frames with calls must also reserve space for 6 outgoing arguments
56 // whether they are used or not. LowerCall_64 takes care of that.
57 assert(frameSize % 16 == 0 && "Stack size not 16-byte aligned");
58 } else {
59 // Emit the correct save instruction based on the number of bytes in
60 // the frame. Minimum stack frame size according to V8 ABI is:
61 // 16 words for register window spill
62 // 1 word for address of returned aggregate-value
63 // + 6 words for passing parameters on the stack
64 // ----------
65 // 23 words * 4 bytes per word = 92 bytes
66 frameSize += 92;
67
68 // Round up to next doubleword boundary -- a double-word boundary
69 // is required by the ABI.
70 frameSize = RoundUpToAlignment(frameSize, 8);
71 }
72 return frameSize;
73 }
5757 int64_t getStackPointerBias() const {
5858 return is64Bit() ? 2047 : 0;
5959 }
60
61 /// Given a actual stack size as determined by FrameInfo, this function
62 /// returns adjusted framesize which includes space for register window
63 /// spills and arguments.
64 int getAdjustedFrameSize(int stackSize) const;
65
6066 };
6167
6268 } // end namespace llvm
5454 %6 = add nsw i32 %5, %h
5555 ret i32 %6
5656 }
57
58 ; CHECK: leaf_proc_with_local_array:
59 ; CHECK: add %sp, -104, %sp
60 ; CHECK: or %g0, 1, [[R1:%[go][0-7]]]
61 ; CHECK: st [[R1]], [%sp+96]
62 ; CHECK: or %g0, 2, [[R2:%[go][0-7]]]
63 ; CHECK: st [[R2]], [%sp+100]
64 ; CHECK: ld {{.+}}, %o0
65 ; CHECK: jmp %o7+8
66 ; CHECK-NEXT: add %sp, 104, %sp
67
68 define i32 @leaf_proc_with_local_array(i32 %a, i32 %b, i32 %c) {
69 entry:
70 %array = alloca [2 x i32], align 4
71 %0 = sub nsw i32 %b, %c
72 %1 = getelementptr inbounds [2 x i32]* %array, i32 0, i32 0
73 store i32 1, i32* %1, align 4
74 %2 = getelementptr inbounds [2 x i32]* %array, i32 0, i32 1
75 store i32 2, i32* %2, align 4
76 %3 = getelementptr inbounds [2 x i32]* %array, i32 0, i32 %a
77 %4 = load i32* %3, align 4
78 ret i32 %4
79 }