Skip to content

Commit 8418e7b

Browse files
vext01ptersilie
authored andcommitted
Turn GEP instructions into PtrAdds.
Co-authored-by: Edd Barrett <[email protected]>
1 parent 5bd64cb commit 8418e7b

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

llvm/lib/YkIR/YkIRWriter.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ enum OpCode {
3939
Store,
4040
Alloca,
4141
Call,
42-
GetElementPtr,
4342
Br,
4443
CondBr,
4544
ICmp,
4645
BinaryOperator,
4746
Ret,
4847
InsertValue,
48+
PtrAdd,
4949
UnimplementedInstruction = 255, // YKFIXME: Will eventually be deleted.
5050
};
5151

@@ -124,6 +124,7 @@ class YkIRWriter {
124124
private:
125125
Module &M;
126126
MCStreamer &OutStreamer;
127+
DataLayout DL;
127128

128129
vector<llvm::Type *> Types;
129130
vector<llvm::Constant *> Constants;
@@ -326,6 +327,40 @@ class YkIRWriter {
326327
}
327328
}
328329

330+
void serialiseGetElementPtr(GetElementPtrInst *I, ValueLoweringMap &VLMap,
331+
unsigned BBIdx, unsigned &InstIdx) {
332+
unsigned BitWidth = 64;
333+
MapVector<Value *, APInt> Offsets;
334+
APInt Offset(BitWidth, 0);
335+
336+
bool Res = I->collectOffset(DL, BitWidth, Offsets, Offset);
337+
assert(Res);
338+
339+
// type_index:
340+
OutStreamer.emitSizeT(typeIndex(I->getType()));
341+
// opcode:
342+
serialiseOpcode(OpCode::PtrAdd);
343+
// num_operands:
344+
OutStreamer.emitInt32(2);
345+
// pointer:
346+
serialiseOperand(I, VLMap, I->getPointerOperand());
347+
// offset:
348+
serialiseOperand(I, VLMap, ConstantInt::get(I->getContext(), Offset));
349+
350+
VLMap[I] = {BBIdx, InstIdx};
351+
InstIdx++;
352+
}
353+
354+
void serialiseStore(StoreInst *I, ValueLoweringMap &VLMap, unsigned BBIdx,
355+
unsigned &InstIdx)
356+
{
357+
if (I->getNumOperands() == 2) {
358+
serialiseInstGeneric(I, VLMap, BBIdx, InstIdx, OpCode::Store);
359+
} else {
360+
serialiseUnimplementedInstruction(I, VLMap, BBIdx, InstIdx);
361+
}
362+
}
363+
329364
void serialiseInst(Instruction *I, ValueLoweringMap &VLMap, unsigned BBIdx,
330365
unsigned &InstIdx) {
331366
// Macros to help dispatch to serialisers.
@@ -344,7 +379,6 @@ class YkIRWriter {
344379

345380
GENERIC_INST_SERIALISE(I, LoadInst, Load)
346381
GENERIC_INST_SERIALISE(I, StoreInst, Store)
347-
GENERIC_INST_SERIALISE(I, GetElementPtrInst, GetElementPtr)
348382
GENERIC_INST_SERIALISE(I, ICmpInst, ICmp)
349383
GENERIC_INST_SERIALISE(I, llvm::BinaryOperator, BinaryOperator)
350384
GENERIC_INST_SERIALISE(I, ReturnInst, Ret)
@@ -353,6 +387,8 @@ class YkIRWriter {
353387
CUSTOM_INST_SERIALISE(I, AllocaInst, serialiseAllocaInst)
354388
CUSTOM_INST_SERIALISE(I, CallInst, serialiseCallInst)
355389
CUSTOM_INST_SERIALISE(I, BranchInst, serialiseBranchInst)
390+
CUSTOM_INST_SERIALISE(I, GetElementPtrInst, serialiseGetElementPtr)
391+
CUSTOM_INST_SERIALISE(I, StoreInst, serialiseStore)
356392

357393
// GENERIC_INST_SERIALISE and CUSTOM_INST_SERIALISE do an early return upon
358394
// a match, so if we get here then the instruction wasn't handled.
@@ -487,7 +523,7 @@ class YkIRWriter {
487523

488524
public:
489525
YkIRWriter(Module &M, MCStreamer &OutStreamer)
490-
: M(M), OutStreamer(OutStreamer) {}
526+
: M(M), OutStreamer(OutStreamer), DL(&M) {}
491527

492528
// Entry point for IR serialisation.
493529
//

0 commit comments

Comments
 (0)