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