Reputation: 394
let's assume I have a simple IR that looks like this:
%1 = alloca i8*, align 8
%2 = bitcast i8* %1 to i8*
%3 = load i8*, i8** %1, align 8
So I am trying to replace the use of %1
in %3 = load i8*, %1, align 8
with the instruction %2 = bitcast i8* %1 to i8*
. I understand how to replace using the replaceUsesWithIf
API, but the problem is that after using that API, I end up with the following
%3 = load i8*, i8* %2, align 8
This is incorrect because the type
that I am load
ing from should be i8**
rather than i8*
.
Therefore, I think I need to obtain a pointer to a BitCast
instruction. I believe one way could be to create a new BitCast
instruction of %2 = bitcast i8* %1 to i8*
instruction with i8**
type?
Here is my attempt at doing so:
for (auto &Inst : BB) {
if (auto bitcast_I = dyn_cast<BitCastInst>(&Inst)) {
/* %2 = bitcast i8* %1 to i8* will be matched */
Value *bitcast_v = bitcastI->getOperand(0);
BitCastInst *newBitcast = new BitCastInst(bitcast_v,
PointerType::get(IntegerType::get(context, 8), 0), "newBit", bitcast_I);
}
Unfortunately, what I attempted above doesn't yield the correct code, but at the same time, I'm not even too sure whether this is right, so I wanted to ask whether this approach makes sense.
Thank you in advance,
Upvotes: 0
Views: 486
Reputation: 1342
Your values have different types, %1 being i8** and %2 being i8* so you cannot replace one with the other. If you want to make your load use the bitcast in %2, then you will need to delete the load and build a new LoadInst.
Upvotes: 1