PowerPCInstrInfo has gone away, PPC32 and PPC64 share opcodes.
[oota-llvm.git] / lib / Target / SparcV9 / MachineCodeForInstruction.cpp
1 //===-- MachineCodeForInstruction.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 // Representation of the sequence of machine instructions created for a single
11 // VM instruction.  Additionally records information about hidden and implicit
12 // values used by the machine instructions: about hidden values used by the
13 // machine instructions:
14 // 
15 // "Temporary values" are intermediate values used in the machine instruction
16 // sequence, but not in the VM instruction. Note that such values should be
17 // treated as pure SSA values with no interpretation of their operands (i.e., as
18 // a TmpInstruction object which actually represents such a value).
19 // 
20 // (2) "Implicit uses" are values used in the VM instruction but not in the
21 //     machine instruction sequence
22 // 
23 //===----------------------------------------------------------------------===//
24
25 #include "MachineCodeForInstruction.h"
26 #include "llvm/Function.h"
27 #include "llvm/Instructions.h"
28 #include "llvm/Type.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/CodeGen/MachineFunction.h"
31 #include "MachineFunctionInfo.h"
32 #include "MachineInstrAnnot.h"
33 #include "SparcV9TmpInstr.h"
34 using namespace llvm;
35
36 MachineCodeForInstruction &MachineCodeForInstruction::get(const Instruction *I){
37   MachineFunction &MF = MachineFunction::get(I->getParent()->getParent());
38   return MF.getInfo()->MCFIEntries[I];
39 }
40 void MachineCodeForInstruction::destroy(const Instruction *I) {
41   MachineFunction &MF = MachineFunction::get(I->getParent()->getParent());
42   MF.getInfo()->MCFIEntries.erase(I);
43 }
44
45 void
46 MachineCodeForInstruction::dropAllReferences()
47 {
48   for (unsigned i=0, N=tempVec.size(); i < N; i++)
49     cast<Instruction>(tempVec[i])->dropAllReferences();
50 }
51
52
53 MachineCodeForInstruction::~MachineCodeForInstruction() {
54   // Let go of all uses in temp. instructions
55   dropAllReferences();
56   
57   // Free the Value objects created to hold intermediate values
58   for (unsigned i=0, N=tempVec.size(); i < N; i++)
59     delete tempVec[i];
60   
61   // do not free the MachineInstr objects allocated. they are managed
62   // by the ilist in MachineBasicBlock
63
64   // Free the CallArgsDescriptor if it exists.
65   delete callArgsDesc;
66 }
67
68
69 CallArgsDescriptor::CallArgsDescriptor(CallInst* _callInstr,
70                                        TmpInstruction* _retAddrReg,
71                                        bool _isVarArgs, bool _noPrototype)
72   : callInstr(_callInstr),
73     funcPtr(isa<Function>(_callInstr->getCalledValue())
74             ? NULL : _callInstr->getCalledValue()),
75     retAddrReg(_retAddrReg),
76     isVarArgs(_isVarArgs),
77     noPrototype(_noPrototype) {
78   unsigned int numArgs = callInstr->getNumOperands();
79   argInfoVec.reserve(numArgs);
80   assert(callInstr->getOperand(0) == callInstr->getCalledValue()
81          && "Operand 0 is ignored in the loop below!");
82   for (unsigned int i=1; i < numArgs; ++i)
83     argInfoVec.push_back(CallArgInfo(callInstr->getOperand(i)));
84
85   // Enter this object in the MachineCodeForInstr object of the CallInst.
86   // This transfers ownership of this object.
87   MachineCodeForInstruction::get(callInstr).setCallArgsDescriptor(this); 
88 }
89
90 CallInst *CallArgsDescriptor::getReturnValue() const {
91   return (callInstr->getType() == Type::VoidTy? NULL : callInstr);
92 }
93
94 // Mechanism to get the descriptor for a CALL MachineInstr.
95 // We get the LLVM CallInstr from the ret. addr. register argument
96 // of the CALL MachineInstr (which is explicit operand #3 for indirect
97 // calls or the last implicit operand for direct calls).  We then get
98 // the CallArgsDescriptor from the MachineCodeForInstruction object for
99 // the CallInstr.
100 // This is roundabout but avoids adding a new map or annotation just
101 // to keep track of CallArgsDescriptors.
102 // 
103 CallArgsDescriptor *CallArgsDescriptor::get(const MachineInstr* MI) {
104   const TmpInstruction* retAddrReg =
105     cast<TmpInstruction>(isa<Function>(MI->getOperand(0).getVRegValue())
106                          ? MI->getImplicitRef(MI->getNumImplicitRefs()-1)
107                          : MI->getOperand(2).getVRegValue());
108
109   assert(retAddrReg->getNumOperands() == 1 &&
110          isa<CallInst>(retAddrReg->getOperand(0)) &&
111          "Location of callInstr arg for CALL instr. changed? FIX THIS CODE!");
112
113   const CallInst* callInstr = cast<CallInst>(retAddrReg->getOperand(0));
114
115   CallArgsDescriptor* desc =
116     MachineCodeForInstruction::get(callInstr).getCallArgsDescriptor(); 
117   assert(desc->getCallInst()==callInstr && "Incorrect call args descriptor?");
118   return desc;
119 }