Skip to content

Commit 45bd986

Browse files
authored
Merge pull request rust-lang#130 from vext01/fold-binops
Fold binops into one opcode.
2 parents 05a2cd9 + ddba8c6 commit 45bd986

File tree

1 file changed

+63
-44
lines changed

1 file changed

+63
-44
lines changed

llvm/lib/YkIR/YkIRWriter.cpp

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,7 @@ enum OpCode {
4747
Ret,
4848
InsertValue,
4949
PtrAdd,
50-
Add,
51-
Sub,
52-
Mul,
53-
Or,
54-
And,
55-
Xor,
56-
Shl,
57-
AShr,
58-
FAdd,
59-
FDiv,
60-
FMul,
61-
FRem,
62-
FSub,
63-
LShr,
64-
SDiv,
65-
SRem,
66-
UDiv,
67-
URem,
50+
BinOp,
6851
UnimplementedInstruction = 255, // YKFIXME: Will eventually be deleted.
6952
};
7053

@@ -77,6 +60,7 @@ enum OperandKind {
7760
Arg,
7861
Global,
7962
Predicate,
63+
OpKindBinOp,
8064
UnimplementedOperand = 255,
8165
};
8266

@@ -103,6 +87,28 @@ enum CmpPredicate {
10387
PredSignedLessEqual,
10488
};
10589

90+
// A binary operator.
91+
enum BinOp {
92+
BinOpAdd,
93+
BinOpSub,
94+
BinOpMul,
95+
BinOpOr,
96+
BinOpAnd,
97+
BinOpXor,
98+
BinOpShl,
99+
BinOpAShr,
100+
BinOpFAdd,
101+
BinOpFDiv,
102+
BinOpFMul,
103+
BinOpFRem,
104+
BinOpFSub,
105+
BinOpLShr,
106+
BinOpSDiv,
107+
BinOpSRem,
108+
BinOpUDiv,
109+
BinOpURem,
110+
};
111+
106112
template <class T> string toString(T *X) {
107113
string S;
108114
raw_string_ostream SS(S);
@@ -314,74 +320,87 @@ class YkIRWriter {
314320
void serialiseBinaryOperation(llvm::BinaryOperator *I,
315321
ValueLoweringMap &VLMap, unsigned BBIdx,
316322
unsigned &InstIdx) {
323+
assert(I->getNumOperands() == 2);
324+
325+
// type index:
317326
OutStreamer.emitSizeT(typeIndex(I->getType()));
318-
serialiseBinOpcode(I->getOpcode());
319-
OutStreamer.emitInt32(I->getNumOperands());
320-
for (Value *O : I->operands()) {
321-
serialiseOperand(I, VLMap, O);
322-
}
327+
// opcode:
328+
OutStreamer.emitInt8(OpCode::BinOp);
329+
// num operands:
330+
OutStreamer.emitInt32(3);
331+
// left-hand side:
332+
serialiseOperand(I, VLMap, I->getOperand(0));
333+
// binary operator:
334+
serialiseBinOperatorOperand(I->getOpcode());
335+
// right-hand side:
336+
serialiseOperand(I, VLMap, I->getOperand(1));
337+
323338
VLMap[I] = {BBIdx, InstIdx};
324339
InstIdx++;
325340
}
326341

327-
void serialiseBinOpcode(Instruction::BinaryOps BO) {
342+
// Serialise a binary operator.
343+
void serialiseBinOperatorOperand(Instruction::BinaryOps BO) {
344+
// operand kind:
345+
OutStreamer.emitInt8(OperandKind::OpKindBinOp);
346+
// the operator:
328347
switch (BO) {
329348
case Instruction::BinaryOps::Add:
330-
OutStreamer.emitInt8(OpCode::Add);
349+
OutStreamer.emitInt8(BinOp::BinOpAdd);
331350
break;
332351
case Instruction::BinaryOps::Sub:
333-
OutStreamer.emitInt8(OpCode::Sub);
352+
OutStreamer.emitInt8(BinOp::BinOpSub);
334353
break;
335354
case Instruction::BinaryOps::Mul:
336-
OutStreamer.emitInt8(OpCode::Mul);
355+
OutStreamer.emitInt8(BinOp::BinOpMul);
337356
break;
338357
case Instruction::BinaryOps::Or:
339-
OutStreamer.emitInt8(OpCode::Or);
358+
OutStreamer.emitInt8(BinOp::BinOpOr);
340359
break;
341360
case Instruction::BinaryOps::And:
342-
OutStreamer.emitInt8(OpCode::And);
361+
OutStreamer.emitInt8(BinOp::BinOpAnd);
343362
break;
344363
case Instruction::BinaryOps::Xor:
345-
OutStreamer.emitInt8(OpCode::Xor);
364+
OutStreamer.emitInt8(BinOp::BinOpXor);
346365
break;
347366
case Instruction::BinaryOps::Shl:
348-
OutStreamer.emitInt8(OpCode::Shl);
367+
OutStreamer.emitInt8(BinOp::BinOpShl);
349368
break;
350369
case Instruction::BinaryOps::AShr:
351-
OutStreamer.emitInt8(OpCode::AShr);
370+
OutStreamer.emitInt8(BinOp::BinOpAShr);
352371
break;
353372
case Instruction::BinaryOps::FAdd:
354-
OutStreamer.emitInt8(OpCode::FAdd);
373+
OutStreamer.emitInt8(BinOp::BinOpFAdd);
355374
break;
356375
case Instruction::BinaryOps::FDiv:
357-
OutStreamer.emitInt8(OpCode::FDiv);
376+
OutStreamer.emitInt8(BinOp::BinOpFDiv);
358377
break;
359378
case Instruction::BinaryOps::FMul:
360-
OutStreamer.emitInt8(OpCode::FMul);
379+
OutStreamer.emitInt8(BinOp::BinOpFMul);
361380
break;
362381
case Instruction::BinaryOps::FRem:
363-
OutStreamer.emitInt8(OpCode::FRem);
382+
OutStreamer.emitInt8(BinOp::BinOpFRem);
364383
break;
365384
case Instruction::BinaryOps::FSub:
366-
OutStreamer.emitInt8(OpCode::FSub);
385+
OutStreamer.emitInt8(BinOp::BinOpFSub);
367386
break;
368387
case Instruction::BinaryOps::LShr:
369-
OutStreamer.emitInt8(OpCode::LShr);
388+
OutStreamer.emitInt8(BinOp::BinOpLShr);
370389
break;
371390
case Instruction::BinaryOps::SDiv:
372-
OutStreamer.emitInt8(OpCode::SDiv);
391+
OutStreamer.emitInt8(BinOp::BinOpSDiv);
373392
break;
374393
case Instruction::BinaryOps::SRem:
375-
OutStreamer.emitInt8(OpCode::SRem);
394+
OutStreamer.emitInt8(BinOp::BinOpSRem);
376395
break;
377396
case Instruction::BinaryOps::UDiv:
378-
OutStreamer.emitInt8(OpCode::UDiv);
397+
OutStreamer.emitInt8(BinOp::BinOpUDiv);
379398
break;
380399
case Instruction::BinaryOps::URem:
381-
OutStreamer.emitInt8(OpCode::URem);
382-
break;
383-
case Instruction::BinaryOps::BinaryOpsEnd:
400+
OutStreamer.emitInt8(BinOp::BinOpURem);
384401
break;
402+
default:
403+
llvm::report_fatal_error("unknown binary operator");
385404
}
386405
}
387406

0 commit comments

Comments
 (0)