Chris Lattner | 8b41cba | 2004-08-16 22:38:02 +0000 | [diff] [blame] | 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 | // |
Brian Gaeke | ac9edd5 | 2004-08-24 06:41:38 +0000 | [diff] [blame] | 10 | // 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 Lattner | 8b41cba | 2004-08-16 22:38:02 +0000 | [diff] [blame] | 16 | // |
| 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 Gaeke | ac9edd5 | 2004-08-24 06:41:38 +0000 | [diff] [blame] | 28 | #include "SparcV9RegisterInfo.h" |
Chris Lattner | 8b41cba | 2004-08-16 22:38:02 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | MachineCodeForInstruction &MachineCodeForInstruction::get(const Instruction *I){ |
| 32 | MachineFunction &MF = MachineFunction::get(I->getParent()->getParent()); |
Chris Lattner | a1e51ff | 2004-08-18 18:13:37 +0000 | [diff] [blame] | 33 | return MF.getInfo<SparcV9FunctionInfo>()->MCFIEntries[I]; |
Chris Lattner | 8b41cba | 2004-08-16 22:38:02 +0000 | [diff] [blame] | 34 | } |
Brian Gaeke | ac9edd5 | 2004-08-24 06:41:38 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 8b41cba | 2004-08-16 22:38:02 +0000 | [diff] [blame] | 36 | void MachineCodeForInstruction::destroy(const Instruction *I) { |
| 37 | MachineFunction &MF = MachineFunction::get(I->getParent()->getParent()); |
Chris Lattner | a1e51ff | 2004-08-18 18:13:37 +0000 | [diff] [blame] | 38 | MF.getInfo<SparcV9FunctionInfo>()->MCFIEntries.erase(I); |
Chris Lattner | 8b41cba | 2004-08-16 22:38:02 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Brian Gaeke | ac9edd5 | 2004-08-24 06:41:38 +0000 | [diff] [blame] | 41 | void MachineCodeForInstruction::dropAllReferences() { |
Chris Lattner | 8b41cba | 2004-08-16 22:38:02 +0000 | [diff] [blame] | 42 | for (unsigned i=0, N=tempVec.size(); i < N; i++) |
| 43 | cast<Instruction>(tempVec[i])->dropAllReferences(); |
| 44 | } |
| 45 | |
Chris Lattner | 8b41cba | 2004-08-16 22:38:02 +0000 | [diff] [blame] | 46 | MachineCodeForInstruction::~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 Lattner | 8b41cba | 2004-08-16 22:38:02 +0000 | [diff] [blame] | 61 | CallArgsDescriptor::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 | |
| 82 | CallInst *CallArgsDescriptor::getReturnValue() const { |
| 83 | return (callInstr->getType() == Type::VoidTy? NULL : callInstr); |
| 84 | } |
| 85 | |
Brian Gaeke | ac9edd5 | 2004-08-24 06:41:38 +0000 | [diff] [blame] | 86 | /// 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 | /// |
| 94 | CallArgsDescriptor *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 Lattner | 8b41cba | 2004-08-16 22:38:02 +0000 | [diff] [blame] | 104 | |
Brian Gaeke | ac9edd5 | 2004-08-24 06:41:38 +0000 | [diff] [blame] | 105 | const TmpInstruction* retAddrReg = cast<TmpInstruction> (retAddrVal); |
Chris Lattner | 8b41cba | 2004-08-16 22:38:02 +0000 | [diff] [blame] | 106 | 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 | } |