xref: /llvm-project/llvm/lib/CodeGen/MachineInstr.cpp (revision 27ccb70df68684cb4b9d4dafacb88272c8700c45)
1 //===-- MachineInstr.cpp --------------------------------------------------===//
2 //
3 //===----------------------------------------------------------------------===//
4 
5 #include "llvm/CodeGen/MachineInstr.h"
6 #include "llvm/CodeGen/MachineBasicBlock.h"
7 #include "llvm/Value.h"
8 #include "llvm/Target/MachineInstrInfo.h"  // FIXME: shouldn't need this!
9 using std::cerr;
10 
11 // Global variable holding an array of descriptors for machine instructions.
12 // The actual object needs to be created separately for each target machine.
13 // This variable is initialized and reset by class MachineInstrInfo.
14 //
15 // FIXME: This should be a property of the target so that more than one target
16 // at a time can be active...
17 //
18 extern const MachineInstrDescriptor *TargetInstrDescriptors;
19 
20 // Constructor for instructions with fixed #operands (nearly all)
21 MachineInstr::MachineInstr(MachineOpCode _opCode)
22   : opCode(_opCode),
23     operands(TargetInstrDescriptors[_opCode].numOperands, MachineOperand()),
24     numImplicitRefs(0)
25 {
26   assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
27 }
28 
29 // Constructor for instructions with variable #operands
30 MachineInstr::MachineInstr(MachineOpCode OpCode, unsigned  numOperands)
31   : opCode(OpCode),
32     operands(numOperands, MachineOperand()),
33     numImplicitRefs(0)
34 {
35 }
36 
37 /// MachineInstr ctor - This constructor only does a _reserve_ of the operands,
38 /// not a resize for them.  It is expected that if you use this that you call
39 /// add* methods below to fill up the operands, instead of the Set methods.
40 /// Eventually, the "resizing" ctors will be phased out.
41 ///
42 MachineInstr::MachineInstr(MachineOpCode Opcode, unsigned numOperands,
43                            bool XX, bool YY)
44   : opCode(Opcode),
45     numImplicitRefs(0)
46 {
47   operands.reserve(numOperands);
48 }
49 
50 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the
51 /// MachineInstr is created and added to the end of the specified basic block.
52 ///
53 MachineInstr::MachineInstr(MachineBasicBlock *MBB, MachineOpCode Opcode,
54                            unsigned numOperands)
55   : opCode(Opcode),
56     numImplicitRefs(0)
57 {
58   assert(MBB && "Cannot use inserting ctor with null basic block!");
59   operands.reserve(numOperands);
60   MBB->push_back(this);  // Add instruction to end of basic block!
61 }
62 
63 
64 // OperandComplete - Return true if it's illegal to add a new operand
65 bool MachineInstr::OperandsComplete() const
66 {
67   int NumOperands = TargetInstrDescriptors[opCode].numOperands;
68   if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
69     return true;  // Broken!
70   return false;
71 }
72 
73 
74 //
75 // Support for replacing opcode and operands of a MachineInstr in place.
76 // This only resets the size of the operand vector and initializes it.
77 // The new operands must be set explicitly later.
78 //
79 void MachineInstr::replace(MachineOpCode Opcode, unsigned numOperands)
80 {
81   assert(getNumImplicitRefs() == 0 &&
82          "This is probably broken because implicit refs are going to be lost.");
83   opCode = Opcode;
84   operands.clear();
85   operands.resize(numOperands, MachineOperand());
86 }
87 
88 void
89 MachineInstr::SetMachineOperandVal(unsigned i,
90                                    MachineOperand::MachineOperandType opType,
91                                    Value* V,
92                                    bool isdef,
93                                    bool isDefAndUse)
94 {
95   assert(i < operands.size());          // may be explicit or implicit op
96   operands[i].opType = opType;
97   operands[i].value = V;
98   operands[i].regNum = -1;
99   operands[i].flags = 0;
100 
101   if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
102     operands[i].markDef();
103   if (isDefAndUse)
104     operands[i].markDefAndUse();
105 }
106 
107 void
108 MachineInstr::SetMachineOperandConst(unsigned i,
109 				MachineOperand::MachineOperandType operandType,
110                                      int64_t intValue)
111 {
112   assert(i < getNumOperands());          // must be explicit op
113   assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
114          "immed. constant cannot be defined");
115 
116   operands[i].opType = operandType;
117   operands[i].value = NULL;
118   operands[i].immedVal = intValue;
119   operands[i].regNum = -1;
120   operands[i].flags = 0;
121 }
122 
123 void
124 MachineInstr::SetMachineOperandReg(unsigned i,
125                                    int regNum,
126                                    bool isdef) {
127   assert(i < getNumOperands());          // must be explicit op
128 
129   operands[i].opType = MachineOperand::MO_MachineRegister;
130   operands[i].value = NULL;
131   operands[i].regNum = regNum;
132   operands[i].flags = 0;
133 
134   if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
135     operands[i].markDef();
136   insertUsedReg(regNum);
137 }
138 
139 void
140 MachineInstr::SetRegForOperand(unsigned i, int regNum)
141 {
142   assert(i < getNumOperands());          // must be explicit op
143   operands[i].setRegForValue(regNum);
144   insertUsedReg(regNum);
145 }
146 
147 
148 // Subsitute all occurrences of Value* oldVal with newVal in all operands
149 // and all implicit refs.  If defsOnly == true, substitute defs only.
150 unsigned
151 MachineInstr::substituteValue(const Value* oldVal, Value* newVal, bool defsOnly)
152 {
153   unsigned numSubst = 0;
154 
155   // Subsitute operands
156   for (MachineInstr::val_op_iterator O = begin(), E = end(); O != E; ++O)
157     if (*O == oldVal)
158       if (!defsOnly || O.isDef())
159         {
160           O.getMachineOperand().value = newVal;
161           ++numSubst;
162         }
163 
164   // Subsitute implicit refs
165   for (unsigned i=0, N=getNumImplicitRefs(); i < N; ++i)
166     if (getImplicitRef(i) == oldVal)
167       if (!defsOnly || implicitRefIsDefined(i))
168         {
169           getImplicitOp(i).value = newVal;
170           ++numSubst;
171         }
172 
173   return numSubst;
174 }
175 
176 
177 void
178 MachineInstr::dump() const
179 {
180   cerr << "  " << *this;
181 }
182 
183 static inline std::ostream&
184 OutputValue(std::ostream &os, const Value* val)
185 {
186   os << "(val ";
187   if (val && val->hasName())
188     return os << val->getName() << ")";
189   else
190     return os << (void*) val << ")";              // print address only
191 }
192 
193 static inline std::ostream&
194 OutputReg(std::ostream &os, unsigned int regNum)
195 {
196   return os << "%mreg(" << regNum << ")";
197 }
198 
199 std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
200 {
201   os << TargetInstrDescriptors[minstr.opCode].Name;
202 
203   for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
204     os << "\t" << minstr.getOperand(i);
205     if( minstr.operandIsDefined(i) )
206       os << "*";
207     if( minstr.operandIsDefinedAndUsed(i) )
208       os << "*";
209   }
210 
211   // code for printing implict references
212   unsigned NumOfImpRefs =  minstr.getNumImplicitRefs();
213   if(  NumOfImpRefs > 0 ) {
214     os << "\tImplicit: ";
215     for(unsigned z=0; z < NumOfImpRefs; z++) {
216       OutputValue(os, minstr.getImplicitRef(z));
217       if( minstr.implicitRefIsDefined(z)) os << "*";
218       if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
219       os << "\t";
220     }
221   }
222 
223   return os << "\n";
224 }
225 
226 std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
227 {
228   if (mop.opHiBits32())
229     os << "%lm(";
230   else if (mop.opLoBits32())
231     os << "%lo(";
232   else if (mop.opHiBits64())
233     os << "%hh(";
234   else if (mop.opLoBits64())
235     os << "%hm(";
236 
237   switch(mop.opType)
238     {
239     case MachineOperand::MO_VirtualRegister:
240       os << "%reg";
241       OutputValue(os, mop.getVRegValue());
242       if (mop.hasAllocatedReg())
243         os << "==" << OutputReg(os, mop.getAllocatedRegNum());
244       break;
245     case MachineOperand::MO_CCRegister:
246       os << "%ccreg";
247       OutputValue(os, mop.getVRegValue());
248       if (mop.hasAllocatedReg())
249         os << "==" << OutputReg(os, mop.getAllocatedRegNum());
250       break;
251     case MachineOperand::MO_MachineRegister:
252       OutputReg(os, mop.getMachineRegNum());
253       break;
254     case MachineOperand::MO_SignExtendedImmed:
255       os << (long)mop.immedVal;
256       break;
257     case MachineOperand::MO_UnextendedImmed:
258       os << (long)mop.immedVal;
259       break;
260     case MachineOperand::MO_PCRelativeDisp:
261       {
262         const Value* opVal = mop.getVRegValue();
263         bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
264         os << "%disp(" << (isLabel? "label " : "addr-of-val ");
265         if (opVal->hasName())
266           os << opVal->getName();
267         else
268           os << (const void*) opVal;
269         os << ")";
270         break;
271       }
272     default:
273       assert(0 && "Unrecognized operand type");
274       break;
275     }
276 
277   if (mop.flags &
278       (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
279        MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
280     os << ")";
281 
282   return os;
283 }
284