Allow emission of names that start with an underscore. This is needed to
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9PrologEpilogInserter.cpp
index b6169c76e8f339d6fe5086cb0e8d3f9e53498236..75146c072ed94d1598080b0fcd1387b4d575ee77 100644 (file)
 #include "llvm/CodeGen/MachineCodeForBasicBlock.h"
 #include "llvm/CodeGen/MachineCodeForInstruction.h"
 #include "llvm/CodeGen/MachineInstr.h"
+#include "llvm/CodeGen/InstrSelectionSupport.h"
 #include "llvm/Pass.h"
 #include "llvm/Function.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/Instruction.h"
 
 namespace {
   class InsertPrologEpilogCode : public FunctionPass {
@@ -59,15 +58,14 @@ void InsertPrologEpilogCode::InsertPrologCode(Function &F)
   
   // The second operand is the stack size. If it does not fit in the
   // immediate field, we have to use a free register to hold the size.
-  // We will assume that local register `l0' is unused since the SAVE
-  // instruction must be the first instruction in each procedure.
+  // See the comments below for the choice of this register.
   // 
   MachineCodeForMethod& mcInfo = MachineCodeForMethod::get(&F);
   unsigned int staticStackSize = mcInfo.getStaticStackSize();
   
   if (staticStackSize < (unsigned) frameInfo.getMinStackFrameSize())
     staticStackSize = (unsigned) frameInfo.getMinStackFrameSize();
-  
+
   if (unsigned padsz = (staticStackSize %
                         (unsigned) frameInfo.getStackFrameSizeAlignment()))
     staticStackSize += frameInfo.getStackFrameSizeAlignment() - padsz;
@@ -83,21 +81,39 @@ void InsertPrologEpilogCode::InsertPrologCode(Function &F)
     }
   else
     {
-      M = new MachineInstr(SETSW);
-      M->SetMachineOperandConst(0, MachineOperand::MO_SignExtendedImmed,
-                                - (int) staticStackSize);
-      M->SetMachineOperandReg(1, MachineOperand::MO_MachineRegister,
-                                 Target.getRegInfo().getUnifiedRegNum(
+      // We have to put the stack size value into a register before SAVE.
+      // Use register %g1 since it is volatile across calls.  Note that the
+      // local (%l) and in (%i) registers cannot be used before the SAVE!
+      // Do this by creating a code sequence equivalent to:
+      //        SETSW -(stackSize), %g1
+      int32_t C = - (int) staticStackSize;
+      int uregNum = Target.getRegInfo().getUnifiedRegNum(
                            Target.getRegInfo().getRegClassIDOfType(Type::IntTy),
-                                  SparcIntRegOrder::l0));
+                           SparcIntRegClass::g1);
+      
+      M = new MachineInstr(SETHI);
+      M->SetMachineOperandConst(0, MachineOperand::MO_SignExtendedImmed, C);
+      M->SetMachineOperandReg(1, uregNum); 
+      M->setOperandHi32(0);
+      mvec.push_back(M);
+      
+      M = new MachineInstr(OR);
+      M->SetMachineOperandReg(0, uregNum);
+      M->SetMachineOperandConst(1, MachineOperand::MO_SignExtendedImmed, C);
+      M->SetMachineOperandReg(2, uregNum);
+      M->setOperandLo32(1);
+      mvec.push_back(M);
+      
+      M = new MachineInstr(SRA);
+      M->SetMachineOperandReg(0, uregNum);
+      M->SetMachineOperandConst(1, MachineOperand::MO_UnextendedImmed, 0);
+      M->SetMachineOperandReg(2, uregNum);
       mvec.push_back(M);
       
+      // Now generate the SAVE using the value in register %g1
       M = new MachineInstr(SAVE);
       M->SetMachineOperandReg(0, Target.getRegInfo().getStackPointer());
-      M->SetMachineOperandReg(1, MachineOperand::MO_MachineRegister,
-                                 Target.getRegInfo().getUnifiedRegNum(
-                           Target.getRegInfo().getRegClassIDOfType(Type::IntTy),
-                                  SparcIntRegOrder::l0));
+      M->SetMachineOperandReg(1, uregNum);
       M->SetMachineOperandReg(2, Target.getRegInfo().getStackPointer());
       mvec.push_back(M);
     }
@@ -146,6 +162,6 @@ void InsertPrologEpilogCode::InsertEpilogCode(Function &F)
   }
 }
 
-Pass *createPrologEpilogCodeInserter(TargetMachine &TM) {
-  return new InsertPrologEpilogCode(TM);
+Pass* UltraSparc::getPrologEpilogInsertionPass() {
+  return new InsertPrologEpilogCode(*this);
 }