blob: 50319ca1b62fcda90cebe6ada9d6a040c278cd52 [file] [log] [blame]
Vikram S. Adve70bc4b52001-07-21 12:41:50 +00001// $Id$
2//***************************************************************************
3// File:
4// MachineInstr.cpp
5//
6// Purpose:
7//
8//
9// Strategy:
10//
11// History:
12// 7/2/01 - Vikram Adve - Created
13//**************************************************************************/
14
Vikram S. Adve5b795912001-08-28 23:02:39 +000015
Chris Lattner822b4fb2001-09-07 17:18:30 +000016#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Advebe495262001-11-08 04:47:06 +000017#include "llvm/Target/MachineFrameInfo.h"
Vikram S. Adve6e447182001-09-18 12:56:28 +000018#include "llvm/Target/MachineRegInfo.h"
Vikram S. Adve5b795912001-08-28 23:02:39 +000019#include "llvm/Method.h"
Vikram S. Advebe495262001-11-08 04:47:06 +000020#include "llvm/iOther.h"
Chris Lattner68498ce2001-07-21 23:24:48 +000021#include "llvm/Instruction.h"
Vikram S. Adve5b795912001-08-28 23:02:39 +000022
Vikram S. Advebe495262001-11-08 04:47:06 +000023AnnotationID MachineCodeForMethod::AID(
24 AnnotationManager::getID("MachineCodeForMethodAnnotation"));
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000025
Ruchira Sasanka69917e22001-10-18 22:40:02 +000026
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000027//************************ Class Implementations **************************/
28
Vikram S. Adve1885da42001-07-31 21:49:28 +000029// Constructor for instructions with fixed #operands (nearly all)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000030MachineInstr::MachineInstr(MachineOpCode _opCode,
31 OpCodeMask _opCodeMask)
32 : opCode(_opCode),
33 opCodeMask(_opCodeMask),
Vikram S. Adve6a175e02001-07-28 04:06:37 +000034 operands(TargetInstrDescriptors[_opCode].numOperands)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000035{
Vikram S. Adve1885da42001-07-31 21:49:28 +000036 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
37}
38
39// Constructor for instructions with variable #operands
40MachineInstr::MachineInstr(MachineOpCode _opCode,
41 unsigned numOperands,
42 OpCodeMask _opCodeMask)
43 : opCode(_opCode),
44 opCodeMask(_opCodeMask),
45 operands(numOperands)
46{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000047}
48
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000049void
50MachineInstr::SetMachineOperand(unsigned int i,
51 MachineOperand::MachineOperandType operandType,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000052 Value* _val, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000053{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000054 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000055 operands[i].Initialize(operandType, _val);
Vikram S. Adve149977b2001-08-13 16:32:45 +000056 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000057 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000058}
59
60void
61MachineInstr::SetMachineOperand(unsigned int i,
62 MachineOperand::MachineOperandType operandType,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000063 int64_t intValue, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000064{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000065 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000066 operands[i].InitializeConst(operandType, intValue);
Vikram S. Adve149977b2001-08-13 16:32:45 +000067 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000068 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000069}
70
71void
72MachineInstr::SetMachineOperand(unsigned int i,
Vikram S. Advedf1c3b82001-11-05 03:56:02 +000073 int regNum, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000074{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000075 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000076 operands[i].InitializeReg(regNum);
Vikram S. Adve149977b2001-08-13 16:32:45 +000077 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000078 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000079}
80
81void
Ruchira Sasanka0b03c6a2001-08-07 21:01:23 +000082MachineInstr::dump(unsigned int indent) const
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000083{
84 for (unsigned i=0; i < indent; i++)
85 cout << " ";
86
87 cout << *this;
88}
89
90ostream&
91operator<< (ostream& os, const MachineInstr& minstr)
92{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000093 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000094
95 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++)
96 os << "\t" << minstr.getOperand(i);
97
Vikram S. Adve6a175e02001-07-28 04:06:37 +000098#undef DEBUG_VAL_OP_ITERATOR
99#ifdef DEBUG_VAL_OP_ITERATOR
100 os << endl << "\tValue operands are: ";
101 for (MachineInstr::val_op_const_iterator vo(&minstr); ! vo.done(); ++vo)
102 {
103 const Value* val = *vo;
104 os << val << (vo.isDef()? "(def), " : ", ");
105 }
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000106#endif
107
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000108
109
110#if 1
111 // code for printing implict references
112
113 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
114 if( NumOfImpRefs > 0 ) {
115
116 os << "\tImplicit:";
117
118 for(unsigned z=0; z < NumOfImpRefs; z++) {
119 os << minstr.getImplicitRef(z);
120 cout << "\t";
121 }
122 }
123
124#endif
125
126
Vikram S. Adve6d353262001-10-17 23:57:50 +0000127 os << endl;
128
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000129 return os;
130}
131
Vikram S. Adve6e447182001-09-18 12:56:28 +0000132static inline ostream&
133OutputOperand(ostream &os, const MachineOperand &mop)
134{
135 switch (mop.getOperandType())
136 {
137 case MachineOperand::MO_CCRegister:
138 case MachineOperand::MO_VirtualRegister:
139 return os << "(val " << mop.getVRegValue() << ")";
140 case MachineOperand::MO_MachineRegister:
141 return os << "(" << mop.getMachineRegNum() << ")";
142 default:
143 assert(0 && "Unknown operand type");
144 return os;
145 }
Chris Lattnere6fdb112001-09-09 22:26:29 +0000146}
147
148
Vikram S. Adve6e447182001-09-18 12:56:28 +0000149ostream&
150operator<<(ostream &os, const MachineOperand &mop)
151{
152 switch(mop.opType)
153 {
154 case MachineOperand::MO_VirtualRegister:
155 case MachineOperand::MO_MachineRegister:
156 os << "%reg";
157 return OutputOperand(os, mop);
158 case MachineOperand::MO_CCRegister:
159 os << "%ccreg";
160 return OutputOperand(os, mop);
161 case MachineOperand::MO_SignExtendedImmed:
162 return os << mop.immedVal;
163 case MachineOperand::MO_UnextendedImmed:
164 return os << mop.immedVal;
165 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000166 {
167 const Value* opVal = mop.getVRegValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000168 bool isLabel = isa<Method>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Advee949da52001-09-30 23:44:19 +0000169 return os << "%disp("
170 << (isLabel? "label " : "addr-of-val ")
171 << opVal << ")";
172 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000173 default:
174 assert(0 && "Unrecognized operand type");
175 break;
176 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000177
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000178 return os;
179}
180
Vikram S. Advebe495262001-11-08 04:47:06 +0000181static unsigned int
182ComputeMaxOptionalArgsSize(const TargetMachine& target, const Method* method)
Vikram S. Adve1d6158f2001-10-22 13:51:33 +0000183{
Vikram S. Advebe495262001-11-08 04:47:06 +0000184 const MachineFrameInfo& frameInfo = target.getFrameInfo();
185
186 unsigned int maxSize = 0;
187
188 for (Method::inst_const_iterator I=method->inst_begin(),E=method->inst_end();
189 I != E; ++I)
190 if ((*I)->getOpcode() == Instruction::Call)
191 {
192 CallInst* callInst = cast<CallInst>(*I);
193 unsigned int numOperands = callInst->getNumOperands() - 1;
Vikram S. Advef1a0a102001-11-11 21:23:33 +0000194 int numExtra = (int) numOperands - frameInfo.getNumFixedOutgoingArgs();
195 if (numExtra <= 0)
196 continue;
Vikram S. Advebe495262001-11-08 04:47:06 +0000197
198 unsigned int sizeForThisCall;
199 if (frameInfo.argsOnStackHaveFixedSize())
200 {
201 int argSize = frameInfo.getSizeOfEachArgOnStack();
202 sizeForThisCall = numExtra * (unsigned) argSize;
203 }
204 else
205 {
206 assert(0 && "UNTESTED CODE: Size per stack argument is not fixed on this architecture: use actual arg sizes to compute MaxOptionalArgsSize");
207 sizeForThisCall = 0;
208 for (unsigned i=0; i < numOperands; ++i)
209 sizeForThisCall += target.findOptimalStorageSize(callInst->
210 getOperand(i)->getType());
211 }
212
213 if (maxSize < sizeForThisCall)
214 maxSize = sizeForThisCall;
215 }
216
217 return maxSize;
Vikram S. Adve1d6158f2001-10-22 13:51:33 +0000218}
219
220
Vikram S. Advebe495262001-11-08 04:47:06 +0000221/*ctor*/
222MachineCodeForMethod::MachineCodeForMethod(const Method* _M,
223 const TargetMachine& target)
224 : Annotation(AID),
225 method(_M), compiledAsLeaf(false), staticStackSize(0),
226 automaticVarsSize(0), regSpillsSize(0),
227 currentOptionalArgsSize(0), maxOptionalArgsSize(0),
228 currentTmpValuesSize(0)
Vikram S. Adve1d6158f2001-10-22 13:51:33 +0000229{
Vikram S. Advebe495262001-11-08 04:47:06 +0000230 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method);
231 staticStackSize = maxOptionalArgsSize +
232 target.getFrameInfo().getMinStackFrameSize();
Vikram S. Adve1d6158f2001-10-22 13:51:33 +0000233}
234
Vikram S. Adve1d6158f2001-10-22 13:51:33 +0000235int
Vikram S. Advebe495262001-11-08 04:47:06 +0000236MachineCodeForMethod::allocateLocalVar(const TargetMachine& target,
237 const Value* val)
Vikram S. Adve1d6158f2001-10-22 13:51:33 +0000238{
Vikram S. Advebe495262001-11-08 04:47:06 +0000239 // Check if we've allocated a stack slot for this value already
240 //
241 int offset = getOffset(val);
242 if (offset == INVALID_FRAME_OFFSET)
243 {
Vikram S. Adve5567e942001-11-12 05:17:23 +0000244 unsigned int size = target.findOptimalStorageSize(val->getType());
245 unsigned char align = target.DataLayout.getTypeAlignment(val->getType());
246
247 offset = getAutomaticVarsSize();
248 if (unsigned int mod = offset % align)
249 {
250 offset += align - mod;
251 size += align - mod;
252 }
253
Vikram S. Advebe495262001-11-08 04:47:06 +0000254 bool growUp;
255 int firstOffset =target.getFrameInfo().getFirstAutomaticVarOffset(*this,
256 growUp);
Vikram S. Adve5567e942001-11-12 05:17:23 +0000257 offset = growUp? firstOffset + offset
258 : firstOffset - offset - size;
Vikram S. Advef1a0a102001-11-11 21:23:33 +0000259
Vikram S. Advebe495262001-11-08 04:47:06 +0000260 offsets[val] = offset;
261
Vikram S. Advebe495262001-11-08 04:47:06 +0000262 incrementAutomaticVarsSize(size);
263 }
264 return offset;
Vikram S. Adve1d6158f2001-10-22 13:51:33 +0000265}
266
Vikram S. Adve1d6158f2001-10-22 13:51:33 +0000267int
Vikram S. Advebe495262001-11-08 04:47:06 +0000268MachineCodeForMethod::allocateSpilledValue(const TargetMachine& target,
269 const Type* type)
Vikram S. Adve1d6158f2001-10-22 13:51:33 +0000270{
Vikram S. Adve5567e942001-11-12 05:17:23 +0000271 unsigned int size = target.findOptimalStorageSize(type);
272 unsigned char align = target.DataLayout.getTypeAlignment(type);
273
274 int offset = getRegSpillsSize();
275 if (unsigned int mod = offset % align)
276 {
277 offset += align - mod;
278 size += align - mod;
279 }
280
Vikram S. Advebe495262001-11-08 04:47:06 +0000281 bool growUp;
282 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
Vikram S. Adve5567e942001-11-12 05:17:23 +0000283 offset = growUp? firstOffset + offset
284 : firstOffset - offset - size;
Vikram S. Advef1a0a102001-11-11 21:23:33 +0000285
Vikram S. Advebe495262001-11-08 04:47:06 +0000286 incrementRegSpillsSize(size);
287
288 return offset;
Vikram S. Adve1d6158f2001-10-22 13:51:33 +0000289}
Vikram S. Advebe495262001-11-08 04:47:06 +0000290
291int
292MachineCodeForMethod::allocateOptionalArg(const TargetMachine& target,
293 const Type* type)
294{
295 const MachineFrameInfo& frameInfo = target.getFrameInfo();
Vikram S. Advef1a0a102001-11-11 21:23:33 +0000296
Vikram S. Advebe495262001-11-08 04:47:06 +0000297 int size = MAXINT;
298 if (frameInfo.argsOnStackHaveFixedSize())
299 size = frameInfo.getSizeOfEachArgOnStack();
300 else
301 {
Vikram S. Advebe495262001-11-08 04:47:06 +0000302 size = target.findOptimalStorageSize(type);
Vikram S. Advef1a0a102001-11-11 21:23:33 +0000303 assert(0 && "UNTESTED CODE: Size per stack argument is not fixed on this architecture: use actual argument sizes for computing optional arg offsets");
Vikram S. Advebe495262001-11-08 04:47:06 +0000304 }
Vikram S. Adve5567e942001-11-12 05:17:23 +0000305 unsigned char align = target.DataLayout.getTypeAlignment(type);
Vikram S. Advebe495262001-11-08 04:47:06 +0000306
Vikram S. Adve5567e942001-11-12 05:17:23 +0000307 int offset = getCurrentOptionalArgsSize();
308 if (unsigned int mod = offset % align)
309 {
310 offset += align - mod;
311 size += align - mod;
312 }
313
314 bool growUp;
315 int firstOffset = frameInfo.getFirstOptionalOutgoingArgOffset(*this, growUp);
316 offset = growUp? firstOffset + offset
317 : firstOffset - offset - size;
318
Vikram S. Advebe495262001-11-08 04:47:06 +0000319 incrementCurrentOptionalArgsSize(size);
320
321 return offset;
322}
323
324void
325MachineCodeForMethod::resetOptionalArgs(const TargetMachine& target)
326{
327 currentOptionalArgsSize = 0;
328}
329
330int
331MachineCodeForMethod::pushTempValue(const TargetMachine& target,
332 unsigned int size)
333{
Vikram S. Adve5567e942001-11-12 05:17:23 +0000334 // Compute a power-of-2 alignment according to the possible sizes,
335 // but not greater than the alignment of the largest type we support
336 // (currently a double word -- see class TargetData).
337 unsigned char align = 1;
338 for (; align < size && align < target.DataLayout.getDoubleAlignment();
339 align = 2*align)
340 ;
341
342 int offset = currentTmpValuesSize;
343 if (unsigned int mod = offset % align)
344 {
345 offset += align - mod;
346 size += align - mod;
347 }
348
Vikram S. Advebe495262001-11-08 04:47:06 +0000349 bool growUp;
350 int firstTmpOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
Vikram S. Adve5567e942001-11-12 05:17:23 +0000351 offset = growUp? firstTmpOffset + offset
352 : firstTmpOffset - offset - size;
353
Vikram S. Advebe495262001-11-08 04:47:06 +0000354 currentTmpValuesSize += size;
355 return offset;
356}
357
358void
359MachineCodeForMethod::popAllTempValues(const TargetMachine& target)
360{
361 currentTmpValuesSize = 0;
362}
363
364
365// void
366// MachineCodeForMethod::putLocalVarAtOffsetFromSP(const Value* local,
367// int offset,
368// unsigned int size)
369// {
370// offsetsFromSP[local] = offset;
371// incrementAutomaticVarsSize(size);
372// }
373//
374
375int
376MachineCodeForMethod::getOffset(const Value* val) const
377{
378 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
379 return (pair == offsets.end())? INVALID_FRAME_OFFSET : (*pair).second;
380}
381
382
383// int
384// MachineCodeForMethod::getOffsetFromSP(const Value* local) const
385// {
386// hash_map<const Value*, int>::const_iterator pair = offsetsFromSP.find(local);
387// return (pair == offsetsFromSP.end())? INVALID_FRAME_OFFSET : (*pair).second;
388// }
Vikram S. Adve1d6158f2001-10-22 13:51:33 +0000389
390
391void
392MachineCodeForMethod::dump() const
Ruchira Sasankaed8f6742001-09-15 19:07:45 +0000393{
394 cout << "\n" << method->getReturnType()
395 << " \"" << method->getName() << "\"" << endl;
396
397 for (Method::const_iterator BI = method->begin(); BI != method->end(); ++BI)
398 {
399 BasicBlock* bb = *BI;
400 cout << "\n"
401 << (bb->hasName()? bb->getName() : "Label")
402 << " (" << bb << ")" << ":"
403 << endl;
404
405 MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
406 for (unsigned i=0; i < mvec.size(); i++)
Vikram S. Adve6d353262001-10-17 23:57:50 +0000407 cout << "\t" << *mvec[i];
Ruchira Sasankaed8f6742001-09-15 19:07:45 +0000408 }
409 cout << endl << "End method \"" << method->getName() << "\""
410 << endl << endl;
411}