Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 1 | //===-- MachineCodeForInstruction.cpp -------------------------------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 51a8d85 | 2002-10-28 01:21:55 +0000 | [diff] [blame] | 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: |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 14 | // |
Chris Lattner | 51a8d85 | 2002-10-28 01:21:55 +0000 | [diff] [blame] | 15 | // "Temporary values" are intermediate values used in the machine instruction |
Brian Gaeke | cdd69e6 | 2004-05-30 03:33:48 +0000 | [diff] [blame] | 16 | // sequence, but not in the VM instruction. Note that such values should be |
Chris Lattner | 51a8d85 | 2002-10-28 01:21:55 +0000 | [diff] [blame] | 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). |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 19 | // |
Chris Lattner | 51a8d85 | 2002-10-28 01:21:55 +0000 | [diff] [blame] | 20 | // (2) "Implicit uses" are values used in the VM instruction but not in the |
| 21 | // machine instruction sequence |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 22 | // |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
Chris Lattner | 4e7244e | 2004-06-27 18:52:17 +0000 | [diff] [blame] | 26 | #include "llvm/Function.h" |
Chris Lattner | e97db86 | 2004-08-16 21:36:31 +0000 | [diff] [blame] | 27 | #include "llvm/Instructions.h" |
| 28 | #include "llvm/Type.h" |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | 4e7244e | 2004-06-27 18:52:17 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineFunction.h" |
| 31 | #include "llvm/CodeGen/MachineFunctionInfo.h" |
Chris Lattner | c81295a | 2004-02-29 21:40:53 +0000 | [diff] [blame] | 32 | #include "../Target/SparcV9/MachineInstrAnnot.h" |
Chris Lattner | e97db86 | 2004-08-16 21:36:31 +0000 | [diff] [blame] | 33 | #include "../Target/SparcV9/SparcV9TmpInstr.h" |
Chris Lattner | d5da197 | 2004-01-09 06:30:18 +0000 | [diff] [blame] | 34 | using namespace llvm; |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 585911e | 2004-02-29 19:02:39 +0000 | [diff] [blame] | 36 | MachineCodeForInstruction &MachineCodeForInstruction::get(const Instruction *I){ |
Chris Lattner | 4e7244e | 2004-06-27 18:52:17 +0000 | [diff] [blame] | 37 | MachineFunction &MF = MachineFunction::get(I->getParent()->getParent()); |
| 38 | return MF.getInfo()->MCFIEntries[I]; |
Chris Lattner | 585911e | 2004-02-29 19:02:39 +0000 | [diff] [blame] | 39 | } |
| 40 | void MachineCodeForInstruction::destroy(const Instruction *I) { |
Chris Lattner | 4e7244e | 2004-06-27 18:52:17 +0000 | [diff] [blame] | 41 | MachineFunction &MF = MachineFunction::get(I->getParent()->getParent()); |
| 42 | MF.getInfo()->MCFIEntries.erase(I); |
Chris Lattner | 585911e | 2004-02-29 19:02:39 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Vikram S. Adve | ded1bf8 | 2002-03-24 03:40:11 +0000 | [diff] [blame] | 45 | void |
| 46 | MachineCodeForInstruction::dropAllReferences() |
| 47 | { |
| 48 | for (unsigned i=0, N=tempVec.size(); i < N; i++) |
Chris Lattner | ae7fc3a | 2004-01-10 19:16:26 +0000 | [diff] [blame] | 49 | cast<Instruction>(tempVec[i])->dropAllReferences(); |
Vikram S. Adve | ded1bf8 | 2002-03-24 03:40:11 +0000 | [diff] [blame] | 50 | } |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 51 | |
Vikram S. Adve | ded1bf8 | 2002-03-24 03:40:11 +0000 | [diff] [blame] | 52 | |
Chris Lattner | d5da197 | 2004-01-09 06:30:18 +0000 | [diff] [blame] | 53 | MachineCodeForInstruction::~MachineCodeForInstruction() { |
Vikram S. Adve | ded1bf8 | 2002-03-24 03:40:11 +0000 | [diff] [blame] | 54 | // Let go of all uses in temp. instructions |
| 55 | dropAllReferences(); |
| 56 | |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 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 | |
Alkis Evlogimenos | c0b9dc5 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 61 | // do not free the MachineInstr objects allocated. they are managed |
| 62 | // by the ilist in MachineBasicBlock |
Vikram S. Adve | e68a343 | 2002-10-29 19:38:46 +0000 | [diff] [blame] | 63 | |
| 64 | // Free the CallArgsDescriptor if it exists. |
Chris Lattner | d5da197 | 2004-01-09 06:30:18 +0000 | [diff] [blame] | 65 | delete callArgsDesc; |
Chris Lattner | f2868ce | 2002-02-03 07:54:50 +0000 | [diff] [blame] | 66 | } |
Chris Lattner | e97db86 | 2004-08-16 21:36:31 +0000 | [diff] [blame] | 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 | } |