diff --git a/lib/Target/CBackend/CBackend.cpp b/lib/Target/CBackend/CBackend.cpp index e9bffdb..aa6c4bd 100644 --- a/lib/Target/CBackend/CBackend.cpp +++ b/lib/Target/CBackend/CBackend.cpp @@ -5366,7 +5366,8 @@ void CWriter::visitAllocaInst(AllocaInst &I) { } void CWriter::printGEPExpression(Value *Ptr, unsigned NumOperands, - gep_type_iterator I, gep_type_iterator E) { + gep_type_iterator I, gep_type_iterator E, + std::optional SourceType) { // If there are no indices, just print out the pointer. if (I == E) { @@ -5397,9 +5398,16 @@ void CWriter::printGEPExpression(Value *Ptr, unsigned NumOperands, Out << "])"; } else { // When the first index is 0 (very common) we can simplify it. - if (tryGetTypeOfAddressExposedValue(Ptr)) { + std::optional ExposedType = tryGetTypeOfAddressExposedValue(Ptr); + if (ExposedType) { // Print P rather than (&P)[0] - Out << "(&"; + Out << '('; + if (SourceType && SourceType.value() != ExposedType.value()) { + Out << "("; + printTypeName(Out, SourceType.value()); + Out << "*)"; + } + Out << '&'; writeOperandInternal(Ptr); Out << ')'; } else if (I != E && I.isStruct()) { @@ -5586,7 +5594,8 @@ void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) { cwriter_assert(++FirstOp == gep_type_end(I)); } else { printGEPExpression(I.getPointerOperand(), I.getNumOperands(), - gep_type_begin(I), gep_type_end(I)); + gep_type_begin(I), gep_type_end(I), + std::optional(I.getSourceElementType())); } } diff --git a/lib/Target/CBackend/CBackend.h b/lib/Target/CBackend/CBackend.h index 85cd33b..2468501 100644 --- a/lib/Target/CBackend/CBackend.h +++ b/lib/Target/CBackend/CBackend.h @@ -339,7 +339,8 @@ class CWriter : public FunctionPass, public InstVisitor { void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock, unsigned Indent); void printGEPExpression(Value *Ptr, unsigned NumOperands, gep_type_iterator I, - gep_type_iterator E); + gep_type_iterator E, + std::optional SourceType = std::nullopt); std::string GetValueName(const Value *Operand);