summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/Scalar/InstructionCombining.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Transforms/Scalar/InstructionCombining.cpp')
-rw-r--r--llvm/lib/Transforms/Scalar/InstructionCombining.cpp58
1 files changed, 47 insertions, 11 deletions
diff --git a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp
index 6c5a16c3615..527d28e2097 100644
--- a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp
@@ -2770,21 +2770,57 @@ Value *InstCombiner::OptimizePointerDifference(Value *LHS, Value *RHS,
// If LHS is a gep based on RHS or RHS is a gep based on LHS, we can optimize
// this.
bool Swapped;
- GetElementPtrInst *GEP;
-
- if ((GEP = dyn_cast<GetElementPtrInst>(LHS)) &&
- GEP->getOperand(0) == RHS)
- Swapped = false;
- else if ((GEP = dyn_cast<GetElementPtrInst>(RHS)) &&
- GEP->getOperand(0) == LHS)
- Swapped = true;
- else
- return 0;
+ GetElementPtrInst *GEP = 0;
+ ConstantExpr *CstGEP = 0;
+
+ // TODO: Could also optimize &A[i] - &A[j] -> "i-j", and "&A.foo[i] - &A.foo".
+ // For now we require one side to be the base pointer "A" or a constant
+ // expression derived from it.
+ if (GetElementPtrInst *LHSGEP = dyn_cast<GetElementPtrInst>(LHS)) {
+ // (gep X, ...) - X
+ if (LHSGEP->getOperand(0) == RHS) {
+ GEP = LHSGEP;
+ Swapped = false;
+ } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(RHS)) {
+ // (gep X, ...) - (ce_gep X, ...)
+ if (CE->getOpcode() == Instruction::GetElementPtr &&
+ LHSGEP->getOperand(0) == CE->getOperand(0)) {
+ CstGEP = CE;
+ GEP = LHSGEP;
+ Swapped = false;
+ }
+ }
+ }
+
+ if (GetElementPtrInst *RHSGEP = dyn_cast<GetElementPtrInst>(RHS)) {
+ // X - (gep X, ...)
+ if (RHSGEP->getOperand(0) == LHS) {
+ GEP = RHSGEP;
+ Swapped = true;
+ } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(LHS)) {
+ // (ce_gep X, ...) - (gep X, ...)
+ if (CE->getOpcode() == Instruction::GetElementPtr &&
+ RHSGEP->getOperand(0) == CE->getOperand(0)) {
+ CstGEP = CE;
+ GEP = RHSGEP;
+ Swapped = true;
+ }
+ }
+ }
- // TODO: Could also optimize &A[i] - &A[j] -> "i-j".
+ if (GEP == 0)
+ return 0;
// Emit the offset of the GEP and an intptr_t.
Value *Result = EmitGEPOffset(GEP, *this);
+
+ // If we had a constant expression GEP on the other side offsetting the
+ // pointer, subtract it from the offset we have.
+ if (CstGEP) {
+ Value *CstOffset = EmitGEPOffset(CstGEP, *this);
+ Result = Builder->CreateSub(Result, CstOffset);
+ }
+
// If we have p - gep(p, ...) then we have to negate the result.
if (Swapped)
OpenPOWER on IntegriCloud