| 1 |
/*========================== begin_copyright_notice ============================ |
| 2 |
|
| 3 |
Copyright (C) 2026 Intel Corporation |
| 4 |
|
| 5 |
SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
============================= end_copyright_notice ===========================*/ |
| 8 |
|
| 9 |
/*========================== begin_copyright_notice ============================ |
| 10 |
|
| 11 |
Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 12 |
See https://llvm.org/LICENSE.txt for license information. |
| 13 |
SPDX-License-Identifier: Apache-2.0 with LLVM-exception |
| 14 |
|
| 15 |
============================= end_copyright_notice ===========================*/ |
| 16 |
|
| 17 |
This PR restores GEP reassociate for loop invariance behaviour which was removed in this commit |
| 18 |
https://github.com/llvm/llvm-project/commit/2ec1d0f427c7822540352c0c14d057e7bfe4f77b |
| 19 |
It serves more as a workaround since it should be done in LICM, but for some reason isn't. |
| 20 |
|
| 21 |
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp |
| 22 |
index 767b7c7de..0995dd836 100644 |
| 23 |
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp |
| 24 |
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp |
| 25 |
@@ -1918,6 +1918,39 @@ Instruction *InstCombinerImpl::visitGEPOfGEP(GetElementPtrInst &GEP, |
| 26 |
if (!shouldMergeGEPs(*cast<GEPOperator>(&GEP), *Src)) |
| 27 |
return nullptr; |
| 28 |
|
| 29 |
+ if (Src->getResultElementType() == GEP.getSourceElementType() && |
| 30 |
+ Src->getNumOperands() == 2 && GEP.getNumOperands() == 2 && |
| 31 |
+ Src->hasOneUse()) { |
| 32 |
+ Value *GO1 = GEP.getOperand(1); |
| 33 |
+ Value *SO1 = Src->getOperand(1); |
| 34 |
+ |
| 35 |
+ if (LI) { |
| 36 |
+ // Try to reassociate loop invariant GEP chains to enable LICM. |
| 37 |
+ if (Loop *L = LI->getLoopFor(GEP.getParent())) { |
| 38 |
+ // Reassociate the two GEPs if SO1 is variant in the loop and GO1 is |
| 39 |
+ // invariant: this breaks the dependence between GEPs and allows LICM |
| 40 |
+ // to hoist the invariant part out of the loop. |
| 41 |
+ if (L->isLoopInvariant(GO1) && !L->isLoopInvariant(SO1)) { |
| 42 |
+ // The swapped GEPs are inbounds if both original GEPs are inbounds |
| 43 |
+ // and the sign of the offsets is the same. For simplicity, only |
| 44 |
+ // handle both offsets being non-negative. |
| 45 |
+ bool IsInBounds = Src->isInBounds() && GEP.isInBounds() && |
| 46 |
+ isKnownNonNegative(SO1, DL, 0, &AC, &GEP, &DT) && |
| 47 |
+ isKnownNonNegative(GO1, DL, 0, &AC, &GEP, &DT); |
| 48 |
+ // Put NewSrc at same location as %src. |
| 49 |
+ Builder.SetInsertPoint(cast<Instruction>(Src)); |
| 50 |
+ Value *NewSrc = Builder.CreateGEP(GEP.getSourceElementType(), |
| 51 |
+ Src->getPointerOperand(), GO1, |
| 52 |
+ Src->getName(), IsInBounds); |
| 53 |
+ GetElementPtrInst *NewGEP = GetElementPtrInst::Create( |
| 54 |
+ GEP.getSourceElementType(), NewSrc, {SO1}); |
| 55 |
+ NewGEP->setIsInBounds(IsInBounds); |
| 56 |
+ return NewGEP; |
| 57 |
+ } |
| 58 |
+ } |
| 59 |
+ } |
| 60 |
+ } |
| 61 |
+ |
| 62 |
// For constant GEPs, use a more general offset-based folding approach. |
| 63 |
Type *PtrTy = Src->getType()->getScalarType(); |
| 64 |
if (GEP.hasAllConstantIndices() && |