MachineInstr.cpp revision 1885da4f49cf32efde2d4c840365c4333a0c8579
170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// $Id$
270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//***************************************************************************
370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// File:
470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//	MachineInstr.cpp
570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//
670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// Purpose:
770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//
870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//
970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// Strategy:
1070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//
1170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// History:
1270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//	7/2/01	 -  Vikram Adve  -  Created
1370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//**************************************************************************/
1470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
1568498cefe602bf5364168b4acd0bd5806cdd72ecChris Lattner#include "llvm/CodeGen/MachineInstr.h"
1670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve#include "llvm/ConstPoolVals.h"
1770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve#include "llvm/Instruction.h"
1868498cefe602bf5364168b4acd0bd5806cdd72ecChris Lattner#include <strstream>
1970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
2070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//************************ Class Implementations **************************/
2170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
221885da4f49cf32efde2d4c840365c4333a0c8579Vikram S. Adve// Constructor for instructions with fixed #operands (nearly all)
2370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. AdveMachineInstr::MachineInstr(MachineOpCode _opCode,
2470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve			   OpCodeMask    _opCodeMask)
2570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  : opCode(_opCode),
2670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    opCodeMask(_opCodeMask),
276a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    operands(TargetInstrDescriptors[_opCode].numOperands)
2870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve{
291885da4f49cf32efde2d4c840365c4333a0c8579Vikram S. Adve  assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
301885da4f49cf32efde2d4c840365c4333a0c8579Vikram S. Adve}
311885da4f49cf32efde2d4c840365c4333a0c8579Vikram S. Adve
321885da4f49cf32efde2d4c840365c4333a0c8579Vikram S. Adve// Constructor for instructions with variable #operands
331885da4f49cf32efde2d4c840365c4333a0c8579Vikram S. AdveMachineInstr::MachineInstr(MachineOpCode _opCode,
341885da4f49cf32efde2d4c840365c4333a0c8579Vikram S. Adve			   unsigned	 numOperands,
351885da4f49cf32efde2d4c840365c4333a0c8579Vikram S. Adve			   OpCodeMask    _opCodeMask)
361885da4f49cf32efde2d4c840365c4333a0c8579Vikram S. Adve  : opCode(_opCode),
371885da4f49cf32efde2d4c840365c4333a0c8579Vikram S. Adve    opCodeMask(_opCodeMask),
381885da4f49cf32efde2d4c840365c4333a0c8579Vikram S. Adve    operands(numOperands)
391885da4f49cf32efde2d4c840365c4333a0c8579Vikram S. Adve{
4070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve}
4170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
4270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Advevoid
4370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. AdveMachineInstr::SetMachineOperand(unsigned int i,
4470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve				MachineOperand::MachineOperandType operandType,
4570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve				Value* _val)
4670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve{
476a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  assert(i < operands.size());
4870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  operands[i].Initialize(operandType, _val);
4970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve}
5070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
5170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Advevoid
5270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. AdveMachineInstr::SetMachineOperand(unsigned int i,
5370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve				MachineOperand::MachineOperandType operandType,
5470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve				int64_t intValue)
5570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve{
566a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  assert(i < operands.size());
5770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  operands[i].InitializeConst(operandType, intValue);
5870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve}
5970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
6070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Advevoid
6170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. AdveMachineInstr::SetMachineOperand(unsigned int i,
6270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve				unsigned int regNum)
6370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve{
646a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  assert(i < operands.size());
6570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  operands[i].InitializeReg(regNum);
6670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve}
6770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
6870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Advevoid
6970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. AdveMachineInstr::dump(unsigned int indent)
7070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve{
7170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  for (unsigned i=0; i < indent; i++)
7270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    cout << "    ";
7370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
7470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  cout << *this;
7570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve}
7670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
7770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adveostream&
7870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adveoperator<< (ostream& os, const MachineInstr& minstr)
7970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve{
806a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  os << TargetInstrDescriptors[minstr.opCode].opCodeString;
8170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
8270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++)
8370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    os << "\t" << minstr.getOperand(i);
8470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
856a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve#undef DEBUG_VAL_OP_ITERATOR
866a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve#ifdef DEBUG_VAL_OP_ITERATOR
876a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  os << endl << "\tValue operands are: ";
886a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  for (MachineInstr::val_op_const_iterator vo(&minstr); ! vo.done(); ++vo)
896a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    {
906a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve      const Value* val = *vo;
916a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve      os << val << (vo.isDef()? "(def), " : ", ");
926a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    }
936a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  os << endl;
946a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve#endif
956a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve
9670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  return os;
9770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve}
9870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
9970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adveostream&
10070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adveoperator<< (ostream& os, const MachineOperand& mop)
10170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve{
10270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  strstream regInfo;
1036a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  if (mop.opType == MachineOperand::MO_VirtualRegister)
1046a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    regInfo << "(val " << mop.value << ")" << ends;
1056a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  else if (mop.opType == MachineOperand::MO_MachineRegister)
1066a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    regInfo << "("       << mop.regNum << ")" << ends;
1076a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  else if (mop.opType == MachineOperand::MO_CCRegister)
10870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    regInfo << "(val " << mop.value << ")" << ends;
10970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
1106a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  switch(mop.opType)
11170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    {
1126a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    case MachineOperand::MO_VirtualRegister:
1136a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    case MachineOperand::MO_MachineRegister:
11470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      os << "%reg" << regInfo.str();
11570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      free(regInfo.str());
11670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      break;
11770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
11870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    case MachineOperand::MO_CCRegister:
11970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      os << "%ccreg" << regInfo.str();
12070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      free(regInfo.str());
12170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      break;
12270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
12370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    case MachineOperand::MO_SignExtendedImmed:
12470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      os << mop.immedVal;
12570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      break;
12670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
12770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    case MachineOperand::MO_UnextendedImmed:
12870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      os << mop.immedVal;
12970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      break;
13070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
13170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    case MachineOperand::MO_PCRelativeDisp:
13270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      os << "%disp(label " << mop.value << ")";
13370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      break;
13470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
13570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    default:
13670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      assert(0 && "Unrecognized operand type");
13770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      break;
13870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    }
13970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
14070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  return os;
14170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve}
14270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
14370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
14470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//---------------------------------------------------------------------------
14570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// Target-independent utility routines for creating machine instructions
14670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//---------------------------------------------------------------------------
14770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
14870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
14970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//------------------------------------------------------------------------
15070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// Function Set2OperandsFromInstr
15170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// Function Set3OperandsFromInstr
15270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//
15370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// For the common case of 2- and 3-operand arithmetic/logical instructions,
15470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// set the m/c instr. operands directly from the VM instruction's operands.
15570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// Check whether the first or second operand is 0 and can use a dedicated "0" register.
15670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// Check whether the second operand should use an immediate field or register.
15770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// (First and third operands are never immediates for such instructions.)
15870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//
15970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// Arguments:
16070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// canDiscardResult: Specifies that the result operand can be discarded
16170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//		     by using the dedicated "0"
16270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//
16370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// op1position, op2position and resultPosition: Specify in which position
16470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//		     in the machine instruction the 3 operands (arg1, arg2
16570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//		     and result) should go.
16670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//
16770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve// RETURN VALUE: unsigned int flags, where
16870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//	flags & 0x01	=> operand 1 is constant and needs a register
16970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//	flags & 0x02	=> operand 2 is constant and needs a register
17070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve//------------------------------------------------------------------------
17170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
17270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Advevoid
17370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. AdveSet2OperandsFromInstr(MachineInstr* minstr,
17470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		      InstructionNode* vmInstrNode,
1756a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve		      const TargetMachine& target,
17670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		      bool canDiscardResult,
17770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		      int op1Position,
17870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		      int resultPosition)
17970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve{
1806a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  Set3OperandsFromInstr(minstr, vmInstrNode, target,
18170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve			canDiscardResult, op1Position,
18270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve			/*op2Position*/ -1, resultPosition);
18370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve}
18470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
1856a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve#undef REVERT_TO_EXPLICIT_CONSTANT_CHECKS
1866a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve#ifdef REVERT_TO_EXPLICIT_CONSTANT_CHECKS
18770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adveunsigned
18870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. AdveSet3OperandsFromInstrJUNK(MachineInstr* minstr,
18970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		      InstructionNode* vmInstrNode,
1906a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve		      const TargetMachine& target,
19170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		      bool canDiscardResult,
19270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		      int op1Position,
19370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		      int op2Position,
19470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		      int resultPosition)
19570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve{
19670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  assert(op1Position >= 0);
19770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  assert(resultPosition >= 0);
19870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
19970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  unsigned returnFlags = 0x0;
20070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
20170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  // Check if operand 1 is 0 and if so, try to use the register that gives 0, if any.
20270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  Value* op1Value = vmInstrNode->leftChild()->getValue();
20370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  bool isValidConstant;
20470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  int64_t intValue = GetConstantValueAsSignedInt(op1Value, isValidConstant);
2056a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  if (isValidConstant && intValue == 0 && target.zeroRegNum >= 0)
2066a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    minstr->SetMachineOperand(op1Position, /*regNum*/ target.zeroRegNum);
20770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  else
20870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    {
20970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      if (op1Value->getValueType() == Value::ConstantVal)
21070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve	{// value is constant and must be loaded from constant pool
21170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve	  returnFlags = returnFlags | (1 << op1Position);
21270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve	}
2136a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve      minstr->SetMachineOperand(op1Position,MachineOperand::MO_VirtualRegister,
2146a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve					    op1Value);
21570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    }
21670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
21770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  // Check if operand 2 (if any) fits in the immediate field of the instruction,
21870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  // of if it is 0 and can use a dedicated machine register
21970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  if (op2Position >= 0)
22070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    {
22170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      Value* op2Value = vmInstrNode->rightChild()->getValue();
22270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      int64_t immedValue;
22370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      unsigned int machineRegNum;
22470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
22570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      MachineOperand::MachineOperandType
2266a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve	op2type = ChooseRegOrImmed(op2Value, minstr->getOpCode(), target,
22770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve				   /*canUseImmed*/ true,
2286a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve				   machineRegNum, immedValue);
22970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
2306a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve      if (op2type == MachineOperand::MO_MachineRegister)
2316a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve	minstr->SetMachineOperand(op2Position, machineRegNum);
2326a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve      else if (op2type == MachineOperand::MO_VirtualRegister)
23370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve	{
2346a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve	  if (op2Value->getValueType() == Value::ConstantVal)
2356a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve	    {// value is constant and must be loaded from constant pool
2366a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve	      returnFlags = returnFlags | (1 << op2Position);
23770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve	    }
2386a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve	  minstr->SetMachineOperand(op2Position, op2type, op2Value);
23970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve	}
24070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      else
2416a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve	{
2426a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve	  assert(op2type != MO_CCRegister);
2436a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve	  minstr->SetMachineOperand(op2Position, op2type, immedValue);
2446a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve	}
24570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    }
24670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
24770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  // If operand 3 (result) can be discarded, use a dead register if one exists
2486a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  if (canDiscardResult && target.zeroRegNum >= 0)
2496a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    minstr->SetMachineOperand(resultPosition, target.zeroRegNum);
25070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  else
2516a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    minstr->SetMachineOperand(resultPosition, MachineOperand::MO_VirtualRegister, vmInstrNode->getValue());
25270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
25370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  return returnFlags;
25470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve}
2556a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve#endif
25670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
25770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
25870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Advevoid
25970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. AdveSet3OperandsFromInstr(MachineInstr* minstr,
26070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		      InstructionNode* vmInstrNode,
2616a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve		      const TargetMachine& target,
26270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		      bool canDiscardResult,
26370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		      int op1Position,
26470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		      int op2Position,
26570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		      int resultPosition)
26670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve{
26770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  assert(op1Position >= 0);
26870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  assert(resultPosition >= 0);
26970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
27070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  // operand 1
2716a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  minstr->SetMachineOperand(op1Position, MachineOperand::MO_VirtualRegister,
27270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve			    vmInstrNode->leftChild()->getValue());
27370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
27470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  // operand 2 (if any)
27570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  if (op2Position >= 0)
2766a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    minstr->SetMachineOperand(op2Position, MachineOperand::MO_VirtualRegister,
27770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve			      vmInstrNode->rightChild()->getValue());
27870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
27970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  // result operand: if it can be discarded, use a dead register if one exists
2806a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  if (canDiscardResult && target.zeroRegNum >= 0)
2816a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    minstr->SetMachineOperand(resultPosition, target.zeroRegNum);
28270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  else
2836a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    minstr->SetMachineOperand(resultPosition, MachineOperand::MO_VirtualRegister, vmInstrNode->getValue());
28470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve}
28570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
28670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
28770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. AdveMachineOperand::MachineOperandType
28870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. AdveChooseRegOrImmed(Value* val,
28970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		 MachineOpCode opCode,
2906a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve		 const TargetMachine& target,
29170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		 bool canUseImmed,
29270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		 unsigned int& getMachineRegNum,
29370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve		 int64_t& getImmedValue)
29470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve{
2956a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve  MachineOperand::MachineOperandType opType =
2966a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve    MachineOperand::MO_VirtualRegister;
29770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  getMachineRegNum = 0;
29870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  getImmedValue = 0;
29970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
30070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  // Check for the common case first: argument is not constant
30170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  //
30270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  if (val->getValueType() != Value::ConstantVal)
30370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    return opType;
30470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
30570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  // Now get the constant value and check if it fits in the IMMED field.
30670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  // Take advantage of the fact that the max unsigned value will rarely
30770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  // fit into any IMMED field and ignore that case (i.e., cast smaller
30870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  // unsigned constants to signed).
30970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  //
31070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  bool isValidConstant;
31170bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  int64_t intValue = GetConstantValueAsSignedInt(val, isValidConstant);
31270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
31370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  if (isValidConstant)
31470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    {
3156a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve      if (intValue == 0 && target.zeroRegNum >= 0)
31670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve	{
3176a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve	  opType = MachineOperand::MO_MachineRegister;
3186a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve	  getMachineRegNum = target.zeroRegNum;
31970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve	}
32070bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve      else if (canUseImmed &&
3216a175e01eb164baac5cc16311c474ff644ce17c1Vikram S. Adve	       target.getInstrInfo().constantFitsInImmedField(opCode,intValue))
32270bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve	{
32370bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve	  opType = MachineOperand::MO_SignExtendedImmed;
32470bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve	  getImmedValue = intValue;
32570bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve	}
32670bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve    }
32770bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve
32870bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve  return opType;
32970bc4b5d1a3795a8f41be96723cfcbccac8e1671Vikram S. Adve}
330