LLVM API Documentation

MachineCodeForInstruction.cpp

Go to the documentation of this file.
00001 //===-- MachineCodeForInstruction.cpp -------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Container for the sequence of MachineInstrs created for a single
00011 // LLVM Instruction.  MachineCodeForInstruction also tracks temporary values
00012 // (TmpInstruction objects) created during SparcV9 code generation, so that
00013 // they can be deleted when they are no longer needed, and finally, it also
00014 // holds some extra information for 'call' Instructions (using the
00015 // CallArgsDescriptor object, which is also implemented in this file).
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #include "MachineCodeForInstruction.h"
00020 #include "llvm/Function.h"
00021 #include "llvm/Instructions.h"
00022 #include "llvm/Type.h"
00023 #include "llvm/CodeGen/MachineInstr.h"
00024 #include "llvm/CodeGen/MachineFunction.h"
00025 #include "MachineFunctionInfo.h"
00026 #include "MachineInstrAnnot.h"
00027 #include "SparcV9TmpInstr.h"
00028 #include "SparcV9RegisterInfo.h"
00029 using namespace llvm;
00030 
00031 MachineCodeForInstruction &MachineCodeForInstruction::get(const Instruction *I){
00032   MachineFunction &MF = MachineFunction::get(I->getParent()->getParent());
00033   return MF.getInfo<SparcV9FunctionInfo>()->MCFIEntries[I];
00034 }
00035 
00036 void MachineCodeForInstruction::destroy(const Instruction *I) {
00037   MachineFunction &MF = MachineFunction::get(I->getParent()->getParent());
00038   MF.getInfo<SparcV9FunctionInfo>()->MCFIEntries.erase(I);
00039 }
00040 
00041 void MachineCodeForInstruction::dropAllReferences() {
00042   for (unsigned i=0, N=tempVec.size(); i < N; i++)
00043     cast<Instruction>(tempVec[i])->dropAllReferences();
00044 }
00045 
00046 MachineCodeForInstruction::~MachineCodeForInstruction() {
00047   // Let go of all uses in temp. instructions
00048   dropAllReferences();
00049 
00050   // Free the Value objects created to hold intermediate values
00051   for (unsigned i=0, N=tempVec.size(); i < N; i++)
00052     delete tempVec[i];
00053 
00054   // do not free the MachineInstr objects allocated. they are managed
00055   // by the ilist in MachineBasicBlock
00056 
00057   // Free the CallArgsDescriptor if it exists.
00058   delete callArgsDesc;
00059 }
00060 
00061 CallArgsDescriptor::CallArgsDescriptor(CallInst* _callInstr,
00062                                        TmpInstruction* _retAddrReg,
00063                                        bool _isVarArgs, bool _noPrototype)
00064   : callInstr(_callInstr),
00065     funcPtr(isa<Function>(_callInstr->getCalledValue())
00066             ? NULL : _callInstr->getCalledValue()),
00067     retAddrReg(_retAddrReg),
00068     isVarArgs(_isVarArgs),
00069     noPrototype(_noPrototype) {
00070   unsigned int numArgs = callInstr->getNumOperands();
00071   argInfoVec.reserve(numArgs);
00072   assert(callInstr->getOperand(0) == callInstr->getCalledValue()
00073          && "Operand 0 is ignored in the loop below!");
00074   for (unsigned int i=1; i < numArgs; ++i)
00075     argInfoVec.push_back(CallArgInfo(callInstr->getOperand(i)));
00076 
00077   // Enter this object in the MachineCodeForInstr object of the CallInst.
00078   // This transfers ownership of this object.
00079   MachineCodeForInstruction::get(callInstr).setCallArgsDescriptor(this);
00080 }
00081 
00082 CallInst *CallArgsDescriptor::getReturnValue() const {
00083   return (callInstr->getType() == Type::VoidTy? NULL : callInstr);
00084 }
00085 
00086 /// CallArgsDescriptor::get - Mechanism to get the descriptor for a CALL
00087 /// MachineInstr.  We get the LLVM CallInst from the return-address register
00088 /// argument of the CALL MachineInstr (which is explicit operand #2 for
00089 /// indirect calls or the last implicit operand for direct calls).  We then get
00090 /// the CallArgsDescriptor from the MachineCodeForInstruction object for the
00091 /// CallInstr.  This is roundabout but avoids adding a new map or annotation
00092 /// just to keep track of CallArgsDescriptors.
00093 ///
00094 CallArgsDescriptor *CallArgsDescriptor::get(const MachineInstr *MI) {
00095   const Value *retAddrVal = 0;
00096   if ((MI->getOperand (0).getType () == MachineOperand::MO_MachineRegister
00097        && MI->getOperand (0).getReg () == SparcV9::g0)
00098       || (MI->getOperand (0).getType () == MachineOperand::MO_VirtualRegister
00099           && !isa<Function> (MI->getOperand (0).getVRegValue ()))) {
00100     retAddrVal = MI->getOperand (2).getVRegValue ();
00101   } else {
00102     retAddrVal = MI->getImplicitRef (MI->getNumImplicitRefs () - 1);
00103   }
00104 
00105   const TmpInstruction* retAddrReg = cast<TmpInstruction> (retAddrVal);
00106   assert(retAddrReg->getNumOperands() == 1 &&
00107          isa<CallInst>(retAddrReg->getOperand(0)) &&
00108          "Location of callInstr arg for CALL instr. changed? FIX THIS CODE!");
00109 
00110   const CallInst* callInstr = cast<CallInst>(retAddrReg->getOperand(0));
00111 
00112   CallArgsDescriptor* desc =
00113     MachineCodeForInstruction::get(callInstr).getCallArgsDescriptor();
00114   assert(desc->getCallInst()==callInstr && "Incorrect call args descriptor?");
00115   return desc;
00116 }