Remove default operands that are never used
[oota-llvm.git] / lib / CodeGen / MachineInstr.cpp
1 //===-- MachineInstr.cpp --------------------------------------------------===//
2 // 
3 //===----------------------------------------------------------------------===//
4
5 #include "llvm/CodeGen/MachineInstr.h"
6 #include "llvm/Value.h"
7 #include "llvm/Target/MachineInstrInfo.h"  // FIXME: shouldn't need this!
8 using std::cerr;
9
10
11 // Constructor for instructions with fixed #operands (nearly all)
12 MachineInstr::MachineInstr(MachineOpCode _opCode)
13   : opCode(_opCode), opCodeMask(0),
14     operands(TargetInstrDescriptors[_opCode].numOperands, MachineOperand()) {
15   assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
16 }
17
18 // Constructor for instructions with variable #operands
19 MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned  numOperands/*,
20                                                                         OpCodeMask    OpCodeMask*/)
21   : opCode(OpCode), opCodeMask(0/*OpCodeMask*/),
22     operands(numOperands, MachineOperand()) {
23 }
24
25 MachineInstr::MachineInstr(MachineOpCode Opcode, unsigned numOperands,
26                            bool XX, bool YY) : opCode(Opcode), opCodeMask(0) {
27   operands.reserve(numOperands);
28 }
29
30 // OperandComplete - Return true if it's illegal to add a new operand
31 bool MachineInstr::OperandsComplete() const {
32   int NumOperands = TargetInstrDescriptors[opCode].numOperands;
33   if (NumOperands >= 0 && operands.size() >= (unsigned)NumOperands)
34     return true;  // Broken!
35   return false;
36 }
37
38
39 // 
40 // Support for replacing opcode and operands of a MachineInstr in place.
41 // This only resets the size of the operand vector and initializes it.
42 // The new operands must be set explicitly later.
43 // 
44 void MachineInstr::replace(MachineOpCode Opcode, unsigned numOperands,
45                            OpCodeMask Mask) {
46   opCode = Opcode;
47   opCodeMask = Mask;
48   operands.clear();
49   operands.resize(numOperands, MachineOperand());
50 }
51
52 void
53 MachineInstr::SetMachineOperandVal(unsigned i,
54                                    MachineOperand::MachineOperandType opType,
55                                    Value* V,
56                                    bool isdef,
57                                    bool isDefAndUse)
58 {
59   assert(i < operands.size());
60   operands[i].opType = opType;
61   operands[i].value = V;
62   operands[i].regNum = -1;
63   operands[i].flags = 0;
64
65   if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
66     operands[i].markDef();
67   if (isDefAndUse)
68     operands[i].markDefAndUse();
69 }
70
71 void
72 MachineInstr::SetMachineOperandConst(unsigned i,
73                                 MachineOperand::MachineOperandType operandType,
74                                      int64_t intValue)
75 {
76   assert(i < operands.size());
77   assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
78          "immed. constant cannot be defined");
79
80   operands[i].opType = operandType;
81   operands[i].value = NULL;
82   operands[i].immedVal = intValue;
83   operands[i].regNum = -1;
84   operands[i].flags = 0;
85 }
86
87 void
88 MachineInstr::SetMachineOperandReg(unsigned i,
89                                    int regNum,
90                                    bool isdef) {
91   assert(i < operands.size());
92
93   operands[i].opType = MachineOperand::MO_MachineRegister;
94   operands[i].value = NULL;
95   operands[i].regNum = regNum;
96   operands[i].flags = 0;
97
98   if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
99     operands[i].markDef();
100   insertUsedReg(regNum);
101 }
102
103 void
104 MachineInstr::SetRegForOperand(unsigned i, int regNum)
105 {
106   operands[i].setRegForValue(regNum);
107   insertUsedReg(regNum);
108 }
109
110
111 // Subsitute all occurrences of Value* oldVal with newVal in all operands
112 // and all implicit refs.  If defsOnly == true, substitute defs only.
113 unsigned
114 MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
115 {
116   unsigned numSubst = 0;
117
118   // Subsitute operands
119   for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
120     if (*O == oldVal)
121       if (!defsOnly || O.isDef())
122         {
123           O.getMachineOperand().value = newVal;
124           ++numSubst;
125         }
126
127   // Subsitute implicit refs
128   for (unsigned i=0, N=implicitRefs.size(); i < N; ++i)
129     if (getImplicitRef(i) == oldVal)
130       if (!defsOnly || implicitRefIsDefined(i))
131         {
132           implicitRefs[i].Val = newVal;
133           ++numSubst;
134         }
135
136   return numSubst;
137 }
138
139
140 void
141 MachineInstr::dump() const 
142 {
143   cerr << "  " << *this;
144 }
145
146 static inline std::ostream&
147 OutputValue(std::ostream &os, const Value* val)
148 {
149   os << "(val ";
150   if (val && val->hasName())
151     return os << val->getName() << ")";
152   else
153     return os << (void*) val << ")";              // print address only
154 }
155
156 static inline std::ostream&
157 OutputReg(std::ostream &os, unsigned int regNum)
158 {
159   return os << "%mreg(" << regNum << ")";
160 }
161
162 std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
163 {
164   os << TargetInstrDescriptors[minstr.opCode].opCodeString;
165   
166   for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
167     os << "\t" << minstr.getOperand(i);
168     if( minstr.operandIsDefined(i) ) 
169       os << "*";
170     if( minstr.operandIsDefinedAndUsed(i) ) 
171       os << "*";
172   }
173   
174   // code for printing implict references
175   unsigned NumOfImpRefs =  minstr.getNumImplicitRefs();
176   if(  NumOfImpRefs > 0 ) {
177     os << "\tImplicit: ";
178     for(unsigned z=0; z < NumOfImpRefs; z++) {
179       OutputValue(os, minstr.getImplicitRef(z)); 
180       if( minstr.implicitRefIsDefined(z)) os << "*";
181       if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
182       os << "\t";
183     }
184   }
185   
186   return os << "\n";
187 }
188
189 std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
190 {
191   if (mop.opHiBits32())
192     os << "%lm(";
193   else if (mop.opLoBits32())
194     os << "%lo(";
195   else if (mop.opHiBits64())
196     os << "%hh(";
197   else if (mop.opLoBits64())
198     os << "%hm(";
199   
200   switch(mop.opType)
201     {
202     case MachineOperand::MO_VirtualRegister:
203       os << "%reg";
204       OutputValue(os, mop.getVRegValue());
205       if (mop.hasAllocatedReg())
206         os << "==" << OutputReg(os, mop.getAllocatedRegNum());
207       break;
208     case MachineOperand::MO_CCRegister:
209       os << "%ccreg";
210       OutputValue(os, mop.getVRegValue());
211       if (mop.hasAllocatedReg())
212         os << "==" << OutputReg(os, mop.getAllocatedRegNum());
213       break;
214     case MachineOperand::MO_MachineRegister:
215       OutputReg(os, mop.getMachineRegNum());
216       break;
217     case MachineOperand::MO_SignExtendedImmed:
218       os << (long)mop.immedVal;
219       break;
220     case MachineOperand::MO_UnextendedImmed:
221       os << (long)mop.immedVal;
222       break;
223     case MachineOperand::MO_PCRelativeDisp:
224       {
225         const Value* opVal = mop.getVRegValue();
226         bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
227         os << "%disp(" << (isLabel? "label " : "addr-of-val ");
228         if (opVal->hasName())
229           os << opVal->getName();
230         else
231           os << (const void*) opVal;
232         os << ")";
233         break;
234       }
235     default:
236       assert(0 && "Unrecognized operand type");
237       break;
238     }
239   
240   if (mop.flags &
241       (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 | 
242        MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
243     os << ")";
244   
245   return os;
246 }