Skip to content

Instantly share code, notes, and snippets.

@nikic

nikic/gep.diff Secret

Created May 13, 2022 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikic/8a1a82382468c88f19094d4b0a2f547c to your computer and use it in GitHub Desktop.
Save nikic/8a1a82382468c88f19094d4b0a2f547c to your computer and use it in GitHub Desktop.
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 4fc6b4de976b..ca62e6504856 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2057,10 +2057,19 @@ Instruction *InstCombinerImpl::visitGEPOfGEP(GetElementPtrInst &GEP,
return nullptr;
// Convert the total offset back into indices.
+ APInt OrigOffset = Offset;
SmallVector<APInt> ConstIndices =
DL.getGEPIndicesForOffset(BaseType, Offset);
- if (!Offset.isZero() || (!IsFirstType && !ConstIndices[0].isZero()))
+ if (!Offset.isZero() || (!IsFirstType && !ConstIndices[0].isZero())) {
+ // When combining two constant GEPs, fall back to i8 element type.
+ if (Src->hasAllConstantIndices())
+ return replaceInstUsesWith(
+ GEP, Builder.CreateGEP(
+ Builder.getInt8Ty(), Src->getPointerOperand(),
+ Builder.getInt(OrigOffset), "",
+ isMergedGEPInBounds(*Src, *cast<GEPOperator>(&GEP))));
return nullptr;
+ }
SmallVector<Value *> Indices;
append_range(Indices, drop_end(Src->indices(),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment