Split saveCallerSavedRegisters into two methods for clarity, and add comments.
[oota-llvm.git] / lib / CodeGen / MachineInstrAnnot.cpp
1 //===-- MachineInstrAnnot.cpp ---------------------------------------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 // 
10 //  This file defines Annotations used to pass information between code
11 //  generation phases.
12 // 
13 //===----------------------------------------------------------------------===//
14
15 #include "../Target/SparcV9/MachineInstrAnnot.h"
16 #include "../Target/SparcV9/SparcV9TmpInstr.h"
17 #include "llvm/CodeGen/MachineCodeForInstruction.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Type.h"
20 using namespace llvm;
21
22 CallArgsDescriptor::CallArgsDescriptor(CallInst* _callInstr,
23                                        TmpInstruction* _retAddrReg,
24                                        bool _isVarArgs, bool _noPrototype)
25   : callInstr(_callInstr),
26     funcPtr(isa<Function>(_callInstr->getCalledValue())
27             ? NULL : _callInstr->getCalledValue()),
28     retAddrReg(_retAddrReg),
29     isVarArgs(_isVarArgs),
30     noPrototype(_noPrototype) {
31   unsigned int numArgs = callInstr->getNumOperands();
32   argInfoVec.reserve(numArgs);
33   assert(callInstr->getOperand(0) == callInstr->getCalledValue()
34          && "Operand 0 is ignored in the loop below!");
35   for (unsigned int i=1; i < numArgs; ++i)
36     argInfoVec.push_back(CallArgInfo(callInstr->getOperand(i)));
37
38   // Enter this object in the MachineCodeForInstr object of the CallInst.
39   // This transfers ownership of this object.
40   MachineCodeForInstruction::get(callInstr).setCallArgsDescriptor(this); 
41 }
42
43 CallInst *CallArgsDescriptor::getReturnValue() const {
44   return (callInstr->getType() == Type::VoidTy? NULL : callInstr);
45 }
46
47 // Mechanism to get the descriptor for a CALL MachineInstr.
48 // We get the LLVM CallInstr from the ret. addr. register argument
49 // of the CALL MachineInstr (which is explicit operand #3 for indirect
50 // calls or the last implicit operand for direct calls).  We then get
51 // the CallArgsDescriptor from the MachineCodeForInstruction object for
52 // the CallInstr.
53 // This is roundabout but avoids adding a new map or annotation just
54 // to keep track of CallArgsDescriptors.
55 // 
56 CallArgsDescriptor *CallArgsDescriptor::get(const MachineInstr* MI) {
57   const TmpInstruction* retAddrReg =
58     cast<TmpInstruction>(isa<Function>(MI->getOperand(0).getVRegValue())
59                          ? MI->getImplicitRef(MI->getNumImplicitRefs()-1)
60                          : MI->getOperand(2).getVRegValue());
61
62   assert(retAddrReg->getNumOperands() == 1 &&
63          isa<CallInst>(retAddrReg->getOperand(0)) &&
64          "Location of callInstr arg for CALL instr. changed? FIX THIS CODE!");
65
66   const CallInst* callInstr = cast<CallInst>(retAddrReg->getOperand(0));
67
68   CallArgsDescriptor* desc =
69     MachineCodeForInstruction::get(callInstr).getCallArgsDescriptor(); 
70   assert(desc->getCallInst()==callInstr && "Incorrect call args descriptor?");
71   return desc;
72 }