LLVM API Documentation

IA64RegisterInfo.cpp

Go to the documentation of this file.
00001 //===- IA64RegisterInfo.cpp - IA64 Register Information ---------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by Duraid Madina and is distributed under the
00006 // University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the IA64 implementation of the MRegisterInfo class.  This
00011 // file is responsible for the frame pointer elimination optimization on IA64.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "IA64.h"
00016 #include "IA64RegisterInfo.h"
00017 #include "IA64InstrBuilder.h"
00018 #include "IA64MachineFunctionInfo.h"
00019 #include "llvm/Constants.h"
00020 #include "llvm/Type.h"
00021 #include "llvm/CodeGen/ValueTypes.h"
00022 #include "llvm/CodeGen/MachineInstrBuilder.h"
00023 #include "llvm/CodeGen/MachineFunction.h"
00024 #include "llvm/CodeGen/MachineFrameInfo.h"
00025 #include "llvm/CodeGen/MachineLocation.h"
00026 #include "llvm/Target/TargetFrameInfo.h"
00027 #include "llvm/Target/TargetMachine.h"
00028 #include "llvm/Target/TargetOptions.h"
00029 #include "llvm/Support/CommandLine.h"
00030 #include "llvm/ADT/STLExtras.h"
00031 #include <iostream>
00032 using namespace llvm;
00033 
00034 
00035 IA64RegisterInfo::IA64RegisterInfo()
00036   : IA64GenRegisterInfo(IA64::ADJUSTCALLSTACKDOWN, IA64::ADJUSTCALLSTACKUP) {}
00037 
00038 void IA64RegisterInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
00039                                            MachineBasicBlock::iterator MI,
00040                                            unsigned SrcReg, int FrameIdx,
00041                                            const TargetRegisterClass *RC) const{
00042 
00043   if (RC == IA64::FPRegisterClass) {
00044     BuildMI(MBB, MI, IA64::STF_SPILL, 2).addFrameIndex(FrameIdx).addReg(SrcReg);
00045   } else if (RC == IA64::GRRegisterClass) {
00046     BuildMI(MBB, MI, IA64::ST8, 2).addFrameIndex(FrameIdx).addReg(SrcReg);
00047  }
00048   else if (RC == IA64::PRRegisterClass) {
00049     /* we use IA64::r2 as a temporary register for doing this hackery. */
00050     // first we load 0:
00051     BuildMI(MBB, MI, IA64::MOV, 1, IA64::r2).addReg(IA64::r0);
00052     // then conditionally add 1:
00053     BuildMI(MBB, MI, IA64::CADDIMM22, 3, IA64::r2).addReg(IA64::r2)
00054       .addImm(1).addReg(SrcReg);
00055     // and then store it to the stack
00056     BuildMI(MBB, MI, IA64::ST8, 2).addFrameIndex(FrameIdx).addReg(IA64::r2);
00057   } else assert(0 &&
00058       "sorry, I don't know how to store this sort of reg in the stack\n");
00059 }
00060 
00061 void IA64RegisterInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
00062                                             MachineBasicBlock::iterator MI,
00063                                             unsigned DestReg, int FrameIdx,
00064                                             const TargetRegisterClass *RC)const{
00065 
00066   if (RC == IA64::FPRegisterClass) {
00067     BuildMI(MBB, MI, IA64::LDF_FILL, 1, DestReg).addFrameIndex(FrameIdx);
00068   } else if (RC == IA64::GRRegisterClass) {
00069     BuildMI(MBB, MI, IA64::LD8, 1, DestReg).addFrameIndex(FrameIdx);
00070  } else if (RC == IA64::PRRegisterClass) {
00071    // first we load a byte from the stack into r2, our 'predicate hackery'
00072    // scratch reg
00073    BuildMI(MBB, MI, IA64::LD8, 1, IA64::r2).addFrameIndex(FrameIdx);
00074    // then we compare it to zero. If it _is_ zero, compare-not-equal to
00075    // r0 gives us 0, which is what we want, so that's nice.
00076    BuildMI(MBB, MI, IA64::CMPNE, 2, DestReg).addReg(IA64::r2).addReg(IA64::r0);
00077  } else assert(0 &&
00078      "sorry, I don't know how to load this sort of reg from the stack\n");
00079 }
00080 
00081 void IA64RegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
00082                                    MachineBasicBlock::iterator MI,
00083                                    unsigned DestReg, unsigned SrcReg,
00084                                    const TargetRegisterClass *RC) const {
00085 
00086   if(RC == IA64::PRRegisterClass ) // if a bool, we use pseudocode
00087     // (SrcReg) DestReg = cmp.eq.unc(r0, r0)
00088     BuildMI(MBB, MI, IA64::PCMPEQUNC, 3, DestReg).addReg(IA64::r0).addReg(IA64::r0).addReg(SrcReg);
00089   else // otherwise, MOV works (for both gen. regs and FP regs)
00090     BuildMI(MBB, MI, IA64::MOV, 1, DestReg).addReg(SrcReg);
00091 }
00092 
00093 //===----------------------------------------------------------------------===//
00094 // Stack Frame Processing methods
00095 //===----------------------------------------------------------------------===//
00096 
00097 // hasFP - Return true if the specified function should have a dedicated frame
00098 // pointer register.  This is true if the function has variable sized allocas or
00099 // if frame pointer elimination is disabled.
00100 //
00101 static bool hasFP(MachineFunction &MF) {
00102   return NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects();
00103 }
00104 
00105 void IA64RegisterInfo::
00106 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
00107                               MachineBasicBlock::iterator I) const {
00108 
00109   if (hasFP(MF)) {
00110     // If we have a frame pointer, turn the adjcallstackup instruction into a
00111     // 'sub SP, <amt>' and the adjcallstackdown instruction into 'add SP,
00112     // <amt>'
00113     MachineInstr *Old = I;
00114     unsigned Amount = Old->getOperand(0).getImmedValue();
00115     if (Amount != 0) {
00116       // We need to keep the stack aligned properly.  To do this, we round the
00117       // amount of space needed for the outgoing arguments up to the next
00118       // alignment boundary.
00119       unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
00120       Amount = (Amount+Align-1)/Align*Align;
00121 
00122       MachineInstr *New;
00123       if (Old->getOpcode() == IA64::ADJUSTCALLSTACKDOWN) {
00124         New=BuildMI(IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
00125           .addSImm(-Amount);
00126       } else {
00127         assert(Old->getOpcode() == IA64::ADJUSTCALLSTACKUP);
00128         New=BuildMI(IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12)
00129           .addSImm(Amount);
00130       }
00131 
00132       // Replace the pseudo instruction with a new instruction...
00133       MBB.insert(I, New);
00134     }
00135   }
00136 
00137   MBB.erase(I);
00138 }
00139 
00140 void IA64RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const{
00141   unsigned i = 0;
00142   MachineInstr &MI = *II;
00143   MachineBasicBlock &MBB = *MI.getParent();
00144   MachineFunction &MF = *MBB.getParent();
00145 
00146   bool FP = hasFP(MF);
00147 
00148   while (!MI.getOperand(i).isFrameIndex()) {
00149     ++i;
00150     assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
00151   }
00152 
00153   int FrameIndex = MI.getOperand(i).getFrameIndex();
00154 
00155   // choose a base register: ( hasFP? framepointer : stack pointer )
00156   unsigned BaseRegister = FP ? IA64::r5 : IA64::r12;
00157   // Add the base register
00158   MI.SetMachineOperandReg(i, BaseRegister);
00159 
00160   // Now add the frame object offset to the offset from r1.
00161   int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex);
00162 
00163   // If we're not using a Frame Pointer that has been set to the value of the
00164   // SP before having the stack size subtracted from it, then add the stack size
00165   // to Offset to get the correct offset.
00166   Offset += MF.getFrameInfo()->getStackSize();
00167 
00168   // XXX: we use 'r22' as another hack+slash temporary register here :(
00169   if ( Offset <= 8191 && Offset >= -8192) { // smallish offset
00170     //fix up the old:
00171     MI.SetMachineOperandReg(i, IA64::r22);
00172     MI.getOperand(i).setUse(); // mark r22 as being used
00173                                // (the bundler wants to know this)
00174     //insert the new
00175     MachineInstr* nMI=BuildMI(IA64::ADDIMM22, 2, IA64::r22)
00176       .addReg(BaseRegister).addSImm(Offset);
00177     MBB.insert(II, nMI);
00178   } else { // it's big
00179     //fix up the old:
00180     MI.SetMachineOperandReg(i, IA64::r22);
00181     MI.getOperand(i).setUse(); // mark r22 as being used
00182                                // (the bundler wants to know this)
00183     MachineInstr* nMI;
00184     nMI=BuildMI(IA64::MOVLIMM64, 1, IA64::r22).addSImm(Offset);
00185     MBB.insert(II, nMI);
00186     nMI=BuildMI(IA64::ADD, 2, IA64::r22).addReg(BaseRegister)
00187       .addReg(IA64::r22);
00188     MBB.insert(II, nMI);
00189   }
00190 
00191 }
00192 
00193 void IA64RegisterInfo::emitPrologue(MachineFunction &MF) const {
00194   MachineBasicBlock &MBB = MF.front();   // Prolog goes in entry BB
00195   MachineBasicBlock::iterator MBBI = MBB.begin();
00196   MachineFrameInfo *MFI = MF.getFrameInfo();
00197   MachineInstr *MI;
00198   bool FP = hasFP(MF);
00199 
00200   // first, we handle the 'alloc' instruction, that should be right up the
00201   // top of any function
00202   static const unsigned RegsInOrder[96] = { // there are 96 GPRs the
00203                                             // RSE worries about
00204         IA64::r32, IA64::r33, IA64::r34, IA64::r35,
00205         IA64::r36, IA64::r37, IA64::r38, IA64::r39, IA64::r40, IA64::r41,
00206         IA64::r42, IA64::r43, IA64::r44, IA64::r45, IA64::r46, IA64::r47,
00207         IA64::r48, IA64::r49, IA64::r50, IA64::r51, IA64::r52, IA64::r53,
00208         IA64::r54, IA64::r55, IA64::r56, IA64::r57, IA64::r58, IA64::r59,
00209         IA64::r60, IA64::r61, IA64::r62, IA64::r63, IA64::r64, IA64::r65,
00210         IA64::r66, IA64::r67, IA64::r68, IA64::r69, IA64::r70, IA64::r71,
00211         IA64::r72, IA64::r73, IA64::r74, IA64::r75, IA64::r76, IA64::r77,
00212         IA64::r78, IA64::r79, IA64::r80, IA64::r81, IA64::r82, IA64::r83,
00213         IA64::r84, IA64::r85, IA64::r86, IA64::r87, IA64::r88, IA64::r89,
00214         IA64::r90, IA64::r91, IA64::r92, IA64::r93, IA64::r94, IA64::r95,
00215         IA64::r96, IA64::r97, IA64::r98, IA64::r99, IA64::r100, IA64::r101,
00216         IA64::r102, IA64::r103, IA64::r104, IA64::r105, IA64::r106, IA64::r107,
00217         IA64::r108, IA64::r109, IA64::r110, IA64::r111, IA64::r112, IA64::r113,
00218         IA64::r114, IA64::r115, IA64::r116, IA64::r117, IA64::r118, IA64::r119,
00219         IA64::r120, IA64::r121, IA64::r122, IA64::r123, IA64::r124, IA64::r125,
00220         IA64::r126, IA64::r127 };
00221 
00222   unsigned numStackedGPRsUsed=0;
00223   for(int i=0; i<96; i++) {
00224     if(MF.isPhysRegUsed(RegsInOrder[i]))
00225       numStackedGPRsUsed=i+1; // (i+1 and not ++ - consider fn(fp, fp, int)
00226   }
00227 
00228   unsigned numOutRegsUsed=MF.getInfo<IA64FunctionInfo>()->outRegsUsed;
00229 
00230   // XXX FIXME : this code should be a bit more reliable (in case there _isn't_ a pseudo_alloc in the MBB)
00231   unsigned dstRegOfPseudoAlloc;
00232   for(MBBI = MBB.begin(); /*MBBI->getOpcode() != IA64::PSEUDO_ALLOC*/; ++MBBI) {
00233     assert(MBBI != MBB.end());
00234     if(MBBI->getOpcode() == IA64::PSEUDO_ALLOC) {
00235       dstRegOfPseudoAlloc=MBBI->getOperand(0).getReg();
00236       break;
00237     }
00238   }
00239 
00240   MI=BuildMI(IA64::ALLOC,5).addReg(dstRegOfPseudoAlloc).addImm(0).\
00241      addImm(numStackedGPRsUsed).addImm(numOutRegsUsed).addImm(0);
00242   MBB.insert(MBBI, MI);
00243 
00244   // Get the number of bytes to allocate from the FrameInfo
00245   unsigned NumBytes = MFI->getStackSize();
00246 
00247   if (MFI->hasCalls() && !FP) {
00248     // We reserve argument space for call sites in the function immediately on
00249     // entry to the current function.  This eliminates the need for add/sub
00250     // brackets around call sites.
00251     NumBytes += MFI->getMaxCallFrameSize();
00252   }
00253 
00254   if(FP)
00255     NumBytes += 8; // reserve space for the old FP
00256 
00257   // Do we need to allocate space on the stack?
00258   if (NumBytes == 0)
00259     return;
00260 
00261   // Add 16 bytes at the bottom of the stack (scratch area)
00262   // and round the size to a multiple of the alignment.
00263   unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
00264   unsigned Size = 16 + (FP ? 8 : 0);
00265   NumBytes = (NumBytes+Size+Align-1)/Align*Align;
00266 
00267   // Update frame info to pretend that this is part of the stack...
00268   MFI->setStackSize(NumBytes);
00269 
00270   // adjust stack pointer: r12 -= numbytes
00271   if (NumBytes <= 8191) {
00272     MI=BuildMI(IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12).addImm(-NumBytes);
00273     MBB.insert(MBBI, MI);
00274   } else { // we use r22 as a scratch register here
00275     MI=BuildMI(IA64::MOVLIMM64, 1, IA64::r22).addSImm(-NumBytes);
00276     // FIXME: MOVLSI32 expects a _u_32imm
00277     MBB.insert(MBBI, MI);  // first load the decrement into r22
00278     MI=BuildMI(IA64::ADD, 2, IA64::r12).addReg(IA64::r12).addReg(IA64::r22);
00279     MBB.insert(MBBI, MI);  // then add (subtract) it to r12 (stack ptr)
00280   }
00281 
00282   // now if we need to, save the old FP and set the new
00283   if (FP) {
00284     MI = BuildMI(IA64::ST8, 2).addReg(IA64::r12).addReg(IA64::r5);
00285     MBB.insert(MBBI, MI);
00286     // this must be the last instr in the prolog ?  (XXX: why??)
00287     MI = BuildMI(IA64::MOV, 1, IA64::r5).addReg(IA64::r12);
00288     MBB.insert(MBBI, MI);
00289   }
00290 
00291 }
00292 
00293 void IA64RegisterInfo::emitEpilogue(MachineFunction &MF,
00294                                    MachineBasicBlock &MBB) const {
00295   const MachineFrameInfo *MFI = MF.getFrameInfo();
00296   MachineBasicBlock::iterator MBBI = prior(MBB.end());
00297   MachineInstr *MI;
00298   assert(MBBI->getOpcode() == IA64::RET &&
00299          "Can only insert epilog into returning blocks");
00300 
00301   bool FP = hasFP(MF);
00302 
00303   // Get the number of bytes allocated from the FrameInfo...
00304   unsigned NumBytes = MFI->getStackSize();
00305 
00306   //now if we need to, restore the old FP
00307   if (FP)
00308   {
00309     //copy the FP into the SP (discards allocas)
00310     MI=BuildMI(IA64::MOV, 1, IA64::r12).addReg(IA64::r5);
00311     MBB.insert(MBBI, MI);
00312     //restore the FP
00313     MI=BuildMI(IA64::LD8, 1, IA64::r5).addReg(IA64::r5);
00314     MBB.insert(MBBI, MI);
00315   }
00316 
00317   if (NumBytes != 0)
00318   {
00319     if (NumBytes <= 8191) {
00320       MI=BuildMI(IA64::ADDIMM22, 2, IA64::r12).addReg(IA64::r12).addImm(NumBytes);
00321       MBB.insert(MBBI, MI);
00322     } else {
00323       MI=BuildMI(IA64::MOVLIMM64, 1, IA64::r22).addImm(NumBytes);
00324       MBB.insert(MBBI, MI);
00325       MI=BuildMI(IA64::ADD, 2, IA64::r12).addReg(IA64::r12).addReg(IA64::r22);
00326       MBB.insert(MBBI, MI);
00327     }
00328   }
00329 
00330 }
00331 
00332 unsigned IA64RegisterInfo::getRARegister() const {
00333   assert(0 && "What is the return address register");
00334   return 0;
00335 }
00336 
00337 unsigned IA64RegisterInfo::getFrameRegister(MachineFunction &MF) const {
00338   return hasFP(MF) ? IA64::r5 : IA64::r12;
00339 }
00340 
00341 #include "IA64GenRegisterInfo.inc"
00342