blob: c4b2ee5c9f60aac93ab7dd3b9d5005eca2660dbe [file] [log] [blame]
Chris Lattnerf2868ce2002-02-03 07:54:50 +00001//===-- MachineCodeForInstruction.cpp -------------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattnerf2868ce2002-02-03 07:54:50 +00009//
Chris Lattner51a8d852002-10-28 01:21:55 +000010// 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:
Chris Lattnerf2868ce2002-02-03 07:54:50 +000014//
Chris Lattner51a8d852002-10-28 01:21:55 +000015// "Temporary values" are intermediate values used in the machine instruction
Brian Gaekecdd69e62004-05-30 03:33:48 +000016// sequence, but not in the VM instruction. Note that such values should be
Chris Lattner51a8d852002-10-28 01:21:55 +000017// treated as pure SSA values with no interpretation of their operands (i.e., as
18// a TmpInstruction object which actually represents such a value).
Chris Lattnerf2868ce2002-02-03 07:54:50 +000019//
Chris Lattner51a8d852002-10-28 01:21:55 +000020// (2) "Implicit uses" are values used in the VM instruction but not in the
21// machine instruction sequence
Chris Lattnerf2868ce2002-02-03 07:54:50 +000022//
23//===----------------------------------------------------------------------===//
24
25#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner4e7244e2004-06-27 18:52:17 +000026#include "llvm/Function.h"
Chris Lattnere97db862004-08-16 21:36:31 +000027#include "llvm/Instructions.h"
28#include "llvm/Type.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000029#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner4e7244e2004-06-27 18:52:17 +000030#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattnerc81295a2004-02-29 21:40:53 +000032#include "../Target/SparcV9/MachineInstrAnnot.h"
Chris Lattnere97db862004-08-16 21:36:31 +000033#include "../Target/SparcV9/SparcV9TmpInstr.h"
Chris Lattnerd5da1972004-01-09 06:30:18 +000034using namespace llvm;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000035
Chris Lattner585911e2004-02-29 19:02:39 +000036MachineCodeForInstruction &MachineCodeForInstruction::get(const Instruction *I){
Chris Lattner4e7244e2004-06-27 18:52:17 +000037 MachineFunction &MF = MachineFunction::get(I->getParent()->getParent());
38 return MF.getInfo()->MCFIEntries[I];
Chris Lattner585911e2004-02-29 19:02:39 +000039}
40void MachineCodeForInstruction::destroy(const Instruction *I) {
Chris Lattner4e7244e2004-06-27 18:52:17 +000041 MachineFunction &MF = MachineFunction::get(I->getParent()->getParent());
42 MF.getInfo()->MCFIEntries.erase(I);
Chris Lattner585911e2004-02-29 19:02:39 +000043}
44
Vikram S. Adveded1bf82002-03-24 03:40:11 +000045void
46MachineCodeForInstruction::dropAllReferences()
47{
48 for (unsigned i=0, N=tempVec.size(); i < N; i++)
Chris Lattnerae7fc3a2004-01-10 19:16:26 +000049 cast<Instruction>(tempVec[i])->dropAllReferences();
Vikram S. Adveded1bf82002-03-24 03:40:11 +000050}
Chris Lattnerf2868ce2002-02-03 07:54:50 +000051
Vikram S. Adveded1bf82002-03-24 03:40:11 +000052
Chris Lattnerd5da1972004-01-09 06:30:18 +000053MachineCodeForInstruction::~MachineCodeForInstruction() {
Vikram S. Adveded1bf82002-03-24 03:40:11 +000054 // Let go of all uses in temp. instructions
55 dropAllReferences();
56
Chris Lattnerf2868ce2002-02-03 07:54:50 +000057 // 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
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +000061 // do not free the MachineInstr objects allocated. they are managed
62 // by the ilist in MachineBasicBlock
Vikram S. Advee68a3432002-10-29 19:38:46 +000063
64 // Free the CallArgsDescriptor if it exists.
Chris Lattnerd5da1972004-01-09 06:30:18 +000065 delete callArgsDesc;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000066}
Chris Lattnere97db862004-08-16 21:36:31 +000067
68
69CallArgsDescriptor::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
90CallInst *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//
103CallArgsDescriptor *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}