1 //===-- InstructionWriter.cpp - Functions for writing instructions --------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the routines for encoding instruction opcodes to a
13 //===----------------------------------------------------------------------===//
15 #include "WriterInternals.h"
16 #include "llvm/Module.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Instructions.h"
19 #include "Support/Statistic.h"
24 NumInstrs("bytecodewriter", "Number of instructions");
26 NumOversizedInstrs("bytecodewriter", "Number of oversized instructions");
28 BytesOversizedInstrs("bytecodewriter", "Bytes of oversized instructions");
31 NumHugeOperandInstrs("bytecodewriter", "Number of instructions with > 3 operands");
33 NumOversized1OpInstrs("bytecodewriter", "Number of oversized 1 operand instrs");
35 NumOversized2OpInstrs("bytecodewriter", "Number of oversized 2 operand instrs");
37 NumOversized3OpInstrs("bytecodewriter", "Number of oversized 3 operand instrs");
40 NumOversidedBecauseOfTypes("bytecodewriter", "Number of oversized instructions because of their type");
43 typedef unsigned char uchar;
45 // outputInstructionFormat0 - Output those wierd instructions that have a large
46 // number of operands or have large operands themselves...
48 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
50 static void outputInstructionFormat0(const Instruction *I, unsigned Opcode,
51 const SlotCalculator &Table,
52 unsigned Type, std::deque<uchar> &Out) {
54 BytesOversizedInstrs -= Out.size();
56 // Opcode must have top two bits clear...
57 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
58 output_vbr(Type, Out); // Result type
60 unsigned NumArgs = I->getNumOperands();
61 output_vbr(NumArgs + (isa<CastInst>(I) || isa<VANextInst>(I) ||
62 isa<VAArgInst>(I)), Out);
64 for (unsigned i = 0; i < NumArgs; ++i) {
65 int Slot = Table.getSlot(I->getOperand(i));
66 assert(Slot >= 0 && "No slot number for value!?!?");
67 output_vbr((unsigned)Slot, Out);
70 if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
71 int Slot = Table.getSlot(I->getType());
72 assert(Slot != -1 && "Cast return type unknown?");
73 output_vbr((unsigned)Slot, Out);
74 } else if (const VANextInst *VAI = dyn_cast<VANextInst>(I)) {
75 int Slot = Table.getSlot(VAI->getArgType());
76 assert(Slot != -1 && "VarArg argument type unknown?");
77 output_vbr((unsigned)Slot, Out);
80 align32(Out); // We must maintain correct alignment!
81 BytesOversizedInstrs += Out.size();
85 // outputInstrVarArgsCall - Output the absurdly annoying varargs function calls.
86 // This are more annoying than most because the signature of the call does not
87 // tell us anything about the types of the arguments in the varargs portion.
88 // Because of this, we encode (as type 0) all of the argument types explicitly
89 // before the argument value. This really sucks, but you shouldn't be using
90 // varargs functions in your code! *death to printf*!
92 // Format: [opcode] [type] [numargs] [arg0] [arg1] ... [arg<numargs-1>]
94 static void outputInstrVarArgsCall(const Instruction *I, unsigned Opcode,
95 const SlotCalculator &Table, unsigned Type,
96 std::deque<uchar> &Out) {
97 assert(isa<CallInst>(I) || isa<InvokeInst>(I));
98 // Opcode must have top two bits clear...
99 output_vbr(Opcode << 2, Out); // Instruction Opcode ID
100 output_vbr(Type, Out); // Result type (varargs type)
102 const PointerType *PTy = cast<PointerType>(I->getOperand(0)->getType());
103 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
104 unsigned NumParams = FTy->getNumParams();
106 unsigned NumFixedOperands;
107 if (isa<CallInst>(I)) {
108 // Output an operand for the callee and each fixed argument, then two for
109 // each variable argument.
110 NumFixedOperands = 1+NumParams;
112 assert(isa<InvokeInst>(I) && "Not call or invoke??");
113 // Output an operand for the callee and destinations, then two for each
114 // variable argument.
115 NumFixedOperands = 3+NumParams;
117 output_vbr(2 * I->getNumOperands()-NumFixedOperands, Out);
119 // The type for the function has already been emitted in the type field of the
120 // instruction. Just emit the slot # now.
121 for (unsigned i = 0; i != NumFixedOperands; ++i) {
122 int Slot = Table.getSlot(I->getOperand(i));
123 assert(Slot >= 0 && "No slot number for value!?!?");
124 output_vbr((unsigned)Slot, Out);
127 for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) {
128 // Output Arg Type ID
129 int Slot = Table.getSlot(I->getOperand(i)->getType());
130 assert(Slot >= 0 && "No slot number for value!?!?");
131 output_vbr((unsigned)Slot, Out);
133 // Output arg ID itself
134 Slot = Table.getSlot(I->getOperand(i));
135 assert(Slot >= 0 && "No slot number for value!?!?");
136 output_vbr((unsigned)Slot, Out);
138 align32(Out); // We must maintain correct alignment!
142 // outputInstructionFormat1 - Output one operand instructions, knowing that no
143 // operand index is >= 2^12.
145 static void outputInstructionFormat1(const Instruction *I, unsigned Opcode,
146 const SlotCalculator &Table, int *Slots,
147 unsigned Type, std::deque<uchar> &Out) {
148 // bits Instruction format:
149 // --------------------------
150 // 01-00: Opcode type, fixed to 1.
152 // 19-08: Resulting type plane
153 // 31-20: Operand #1 (if set to (2^12-1), then zero operands)
155 unsigned Bits = 1 | (Opcode << 2) | (Type << 8) | (Slots[0] << 20);
156 // cerr << "1 " << IType << " " << Type << " " << Slots[0] << endl;
161 // outputInstructionFormat2 - Output two operand instructions, knowing that no
162 // operand index is >= 2^8.
164 static void outputInstructionFormat2(const Instruction *I, unsigned Opcode,
165 const SlotCalculator &Table, int *Slots,
166 unsigned Type, std::deque<uchar> &Out) {
167 // bits Instruction format:
168 // --------------------------
169 // 01-00: Opcode type, fixed to 2.
171 // 15-08: Resulting type plane
175 unsigned Bits = 2 | (Opcode << 2) | (Type << 8) |
176 (Slots[0] << 16) | (Slots[1] << 24);
177 // cerr << "2 " << IType << " " << Type << " " << Slots[0] << " "
178 // << Slots[1] << endl;
183 // outputInstructionFormat3 - Output three operand instructions, knowing that no
184 // operand index is >= 2^6.
186 static void outputInstructionFormat3(const Instruction *I, unsigned Opcode,
187 const SlotCalculator &Table, int *Slots,
188 unsigned Type, std::deque<uchar> &Out) {
189 // bits Instruction format:
190 // --------------------------
191 // 01-00: Opcode type, fixed to 3.
193 // 13-08: Resulting type plane
198 unsigned Bits = 3 | (Opcode << 2) | (Type << 8) |
199 (Slots[0] << 14) | (Slots[1] << 20) | (Slots[2] << 26);
200 //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " "
201 // << Slots[1] << " " << Slots[2] << endl;
205 void BytecodeWriter::outputInstruction(const Instruction &I) {
206 assert(I.getOpcode() < 62 && "Opcode too big???");
207 unsigned Opcode = I.getOpcode();
209 // Encode 'volatile load' as 62 and 'volatile store' as 63.
210 if (isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile())
212 if (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile())
215 unsigned NumOperands = I.getNumOperands();
217 int Slots[3]; Slots[0] = (1 << 12)-1; // Marker to signify 0 operands
219 for (unsigned i = 0; i != NumOperands; ++i) {
220 int slot = Table.getSlot(I.getOperand(i));
221 assert(slot != -1 && "Broken bytecode!");
222 if (slot > MaxOpSlot) MaxOpSlot = slot;
223 if (i < 3) Slots[i] = slot;
226 // Figure out which type to encode with the instruction. Typically we want
227 // the type of the first parameter, as opposed to the type of the instruction
228 // (for example, with setcc, we always know it returns bool, but the type of
229 // the first param is actually interesting). But if we have no arguments
230 // we take the type of the instruction itself.
233 switch (I.getOpcode()) {
234 case Instruction::Malloc:
235 case Instruction::Alloca:
236 Ty = I.getType(); // Malloc & Alloca ALWAYS want to encode the return type
238 case Instruction::Store:
239 Ty = I.getOperand(1)->getType(); // Encode the pointer type...
240 assert(isa<PointerType>(Ty) && "Store to nonpointer type!?!?");
242 default: // Otherwise use the default behavior...
243 Ty = NumOperands ? I.getOperand(0)->getType() : I.getType();
248 int Slot = Table.getSlot(Ty);
249 assert(Slot != -1 && "Type not available!!?!");
250 Type = (unsigned)Slot;
252 // Make sure that we take the type number into consideration. We don't want
253 // to overflow the field size for the instruction format we select.
255 if (Slot > MaxOpSlot) MaxOpSlot = Slot;
257 // Handle the special case for cast...
258 if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
259 // Cast has to encode the destination type as the second argument in the
260 // packet, or else we won't know what type to cast to!
261 Slots[1] = Table.getSlot(I.getType());
262 assert(Slots[1] != -1 && "Cast return type unknown?");
263 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
265 } else if (const VANextInst *VANI = dyn_cast<VANextInst>(&I)) {
266 Slots[1] = Table.getSlot(VANI->getArgType());
267 assert(Slots[1] != -1 && "va_next return type unknown?");
268 if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
270 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)){// Handle VarArg calls
271 const PointerType *Ty = cast<PointerType>(CI->getCalledValue()->getType());
272 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
273 outputInstrVarArgsCall(CI, Opcode, Table, Type, Out);
276 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {// ... & Invokes
277 const PointerType *Ty = cast<PointerType>(II->getCalledValue()->getType());
278 if (cast<FunctionType>(Ty->getElementType())->isVarArg()) {
279 outputInstrVarArgsCall(II, Opcode, Table, Type, Out);
286 // Decide which instruction encoding to use. This is determined primarily by
287 // the number of operands, and secondarily by whether or not the max operand
288 // will fit into the instruction encoding. More operands == fewer bits per
291 switch (NumOperands) {
294 if (MaxOpSlot < (1 << 12)-1) { // -1 because we use 4095 to indicate 0 ops
295 outputInstructionFormat1(&I, Opcode, Table, Slots, Type, Out);
298 if (Type >= (1 << 12)-1)
299 NumOversidedBecauseOfTypes++;
301 NumOversized1OpInstrs++;
305 if (MaxOpSlot < (1 << 8)) {
306 outputInstructionFormat2(&I, Opcode, Table, Slots, Type, Out);
309 if (Type >= (1 << 8))
310 NumOversidedBecauseOfTypes++;
311 NumOversized2OpInstrs++;
315 if (MaxOpSlot < (1 << 6)) {
316 outputInstructionFormat3(&I, Opcode, Table, Slots, Type, Out);
319 if (Type >= (1 << 6))
320 NumOversidedBecauseOfTypes++;
321 NumOversized3OpInstrs++;
324 ++NumHugeOperandInstrs;
328 // If we weren't handled before here, we either have a large number of
329 // operands or a large operand index that we are referring to.
330 outputInstructionFormat0(&I, Opcode, Table, Type, Out);