Skip to content

Instantly share code, notes, and snippets.

@ranma42
Last active December 2, 2016 15:50
Show Gist options
  • Save ranma42/6076b33be533d9b09e7dfe3aff52d4e8 to your computer and use it in GitHub Desktop.
Save ranma42/6076b33be533d9b09e7dfe3aff52d4e8 to your computer and use it in GitHub Desktop.
Patch to combine GEPs
From fe155df2fd7778c6602696055f7ebd0ddf433c3d Mon Sep 17 00:00:00 2001
From: Andrea Canciani <ranma42@gmail.com>
Date: Fri, 2 Dec 2016 16:21:02 +0100
Subject: [PATCH] Combine GEPs
---
lib/Transforms/InstCombine/InstructionCombining.cpp | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp
index cdbc8eb..2339714 100644
--- a/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1567,12 +1567,18 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
// normalized.
if (SO1->getType() != GO1->getType())
return nullptr;
- // Only do the combine when GO1 and SO1 are both constants. Only in
- // this case, we are sure the cost after the merge is never more than
- // that before the merge.
- if (!isa<Constant>(GO1) || !isa<Constant>(SO1))
+
+ // Only do the combine when we are sure the cost after the
+ // merge is never more than that before the merge.
+
+ // GO1 and SO1 are both constants, combine them
+ if (isa<Constant>(GO1) && isa<Constant>(SO1))
+ Sum = Builder->CreateAdd(SO1, GO1, PtrOp->getName()+".sum");
+ // GO1 = V - SO1, combine them
+ else if (match(GO1, m_Sub(m_Value(Sum), m_Specific(SO1))))
+ ;
+ else
return nullptr;
- Sum = Builder->CreateAdd(SO1, GO1, PtrOp->getName()+".sum");
}
// Update the GEP in place if possible.
--
2.7.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment