blob: 7ad69ad46a1ccd2ea6abdf58e550bd208fa89fd3 [file] [log] [blame]
Chris Lattner8b41cba2004-08-16 22:38:02 +00001//===-- 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//
Brian Gaekeac9edd52004-08-24 06:41:38 +000010// Container for the sequence of MachineInstrs created for a single
11// LLVM Instruction. MachineCodeForInstruction also tracks temporary values
12// (TmpInstruction objects) created during SparcV9 code generation, so that
13// they can be deleted when they are no longer needed, and finally, it also
14// holds some extra information for 'call' Instructions (using the
15// CallArgsDescriptor object, which is also implemented in this file).
Chris Lattner8b41cba2004-08-16 22:38:02 +000016//
17//===----------------------------------------------------------------------===//
18
19#include "MachineCodeForInstruction.h"
20#include "llvm/Function.h"
21#include "llvm/Instructions.h"
22#include "llvm/Type.h"
23#include "llvm/CodeGen/MachineInstr.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "MachineFunctionInfo.h"
26#include "MachineInstrAnnot.h"
27#include "SparcV9TmpInstr.h"
Brian Gaekeac9edd52004-08-24 06:41:38 +000028#include "SparcV9RegisterInfo.h"
Chris Lattner8b41cba2004-08-16 22:38:02 +000029using namespace llvm;
30
31MachineCodeForInstruction &MachineCodeForInstruction::get(const Instruction *I){
32 MachineFunction &MF = MachineFunction::get(I->getParent()->getParent());
Chris Lattnera1e51ff2004-08-18 18:13:37 +000033 return MF.getInfo<SparcV9FunctionInfo>()->MCFIEntries[I];
Chris Lattner8b41cba2004-08-16 22:38:02 +000034}
Brian Gaekeac9edd52004-08-24 06:41:38 +000035
Chris Lattner8b41cba2004-08-16 22:38:02 +000036void MachineCodeForInstruction::destroy(const Instruction *I) {
37 MachineFunction &MF = MachineFunction::get(I->getParent()->getParent());
Chris Lattnera1e51ff2004-08-18 18:13:37 +000038 MF.getInfo<SparcV9FunctionInfo>()->MCFIEntries.erase(I);
Chris Lattner8b41cba2004-08-16 22:38:02 +000039}
40
Brian Gaekeac9edd52004-08-24 06:41:38 +000041void MachineCodeForInstruction::dropAllReferences() {
Chris Lattner8b41cba2004-08-16 22:38:02 +000042 for (unsigned i=0, N=tempVec.size(); i < N; i++)
43 cast<Instruction>(tempVec[i])->dropAllReferences();
44}
45
Chris Lattner8b41cba2004-08-16 22:38:02 +000046MachineCodeForInstruction::~MachineCodeForInstruction() {
47 // Let go of all uses in temp. instructions
48 dropAllReferences();
49
50 // Free the Value objects created to hold intermediate values
51 for (unsigned i=0, N=tempVec.size(); i < N; i++)
52 delete tempVec[i];
53
54 // do not free the MachineInstr objects allocated. they are managed
55 // by the ilist in MachineBasicBlock
56
57 // Free the CallArgsDescriptor if it exists.
58 delete callArgsDesc;
59}
60
Chris Lattner8b41cba2004-08-16 22:38:02 +000061CallArgsDescriptor::CallArgsDescriptor(CallInst* _callInstr,
62 TmpInstruction* _retAddrReg,
63 bool _isVarArgs, bool _noPrototype)
64 : callInstr(_callInstr),
65 funcPtr(isa<Function>(_callInstr->getCalledValue())
66 ? NULL : _callInstr->getCalledValue()),
67 retAddrReg(_retAddrReg),
68 isVarArgs(_isVarArgs),
69 noPrototype(_noPrototype) {
70 unsigned int numArgs = callInstr->getNumOperands();
71 argInfoVec.reserve(numArgs);
72 assert(callInstr->getOperand(0) == callInstr->getCalledValue()
73 && "Operand 0 is ignored in the loop below!");
74 for (unsigned int i=1; i < numArgs; ++i)
75 argInfoVec.push_back(CallArgInfo(callInstr->getOperand(i)));
76
77 // Enter this object in the MachineCodeForInstr object of the CallInst.
78 // This transfers ownership of this object.
79 MachineCodeForInstruction::get(callInstr).setCallArgsDescriptor(this);
80}
81
82CallInst *CallArgsDescriptor::getReturnValue() const {
83 return (callInstr->getType() == Type::VoidTy? NULL : callInstr);
84}
85
Brian Gaekeac9edd52004-08-24 06:41:38 +000086/// CallArgsDescriptor::get - Mechanism to get the descriptor for a CALL
87/// MachineInstr. We get the LLVM CallInst from the return-address register
88/// argument of the CALL MachineInstr (which is explicit operand #2 for
89/// indirect calls or the last implicit operand for direct calls). We then get
90/// the CallArgsDescriptor from the MachineCodeForInstruction object for the
91/// CallInstr. This is roundabout but avoids adding a new map or annotation
92/// just to keep track of CallArgsDescriptors.
93///
94CallArgsDescriptor *CallArgsDescriptor::get(const MachineInstr *MI) {
95 const Value *retAddrVal = 0;
96 if ((MI->getOperand (0).getType () == MachineOperand::MO_MachineRegister
97 && MI->getOperand (0).getReg () == SparcV9::g0)
98 || (MI->getOperand (0).getType () == MachineOperand::MO_VirtualRegister
99 && !isa<Function> (MI->getOperand (0).getVRegValue ()))) {
100 retAddrVal = MI->getOperand (2).getVRegValue ();
101 } else {
102 retAddrVal = MI->getImplicitRef (MI->getNumImplicitRefs () - 1);
103 }
Chris Lattner8b41cba2004-08-16 22:38:02 +0000104
Brian Gaekeac9edd52004-08-24 06:41:38 +0000105 const TmpInstruction* retAddrReg = cast<TmpInstruction> (retAddrVal);
Chris Lattner8b41cba2004-08-16 22:38:02 +0000106 assert(retAddrReg->getNumOperands() == 1 &&
107 isa<CallInst>(retAddrReg->getOperand(0)) &&
108 "Location of callInstr arg for CALL instr. changed? FIX THIS CODE!");
109
110 const CallInst* callInstr = cast<CallInst>(retAddrReg->getOperand(0));
111
112 CallArgsDescriptor* desc =
113 MachineCodeForInstruction::get(callInstr).getCallArgsDescriptor();
114 assert(desc->getCallInst()==callInstr && "Incorrect call args descriptor?");
115 return desc;
116}