[llvm] e714b98 - [InstCombine] Check type compatibility in indexed load fold
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 11 01:16:34 PST 2022
Author: Nikita Popov
Date: 2022-02-11T10:16:27+01:00
New Revision: e714b98fff74a25097fd5a14c61ec5102fd4a1fc
URL: https://github.com/llvm/llvm-project/commit/e714b98fff74a25097fd5a14c61ec5102fd4a1fc
DIFF: https://github.com/llvm/llvm-project/commit/e714b98fff74a25097fd5a14c61ec5102fd4a1fc.diff
LOG: [InstCombine] Check type compatibility in indexed load fold
This fold could use a rewrite to an offset-based implementation,
but for now make sure it doesn't crash with opaque pointers.
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/test/Transforms/InstCombine/opaque-ptr.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index caec4003f5b5a..46af7d8468922 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -105,10 +105,14 @@ static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) {
///
/// If AndCst is non-null, then the loaded value is masked with that constant
/// before doing the comparison. This handles cases like "A[i]&4 == 0".
-Instruction *
-InstCombinerImpl::foldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
- GlobalVariable *GV, CmpInst &ICI,
- ConstantInt *AndCst) {
+Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(
+ LoadInst *LI, GetElementPtrInst *GEP, GlobalVariable *GV, CmpInst &ICI,
+ ConstantInt *AndCst) {
+ if (LI->isVolatile() || LI->getType() != GEP->getResultElementType() ||
+ GV->getValueType() != GEP->getSourceElementType() ||
+ !GV->isConstant() || !GV->hasDefinitiveInitializer())
+ return nullptr;
+
Constant *Init = GV->getInitializer();
if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init))
return nullptr;
@@ -1865,15 +1869,13 @@ Instruction *InstCombinerImpl::foldICmpAndConstant(ICmpInst &Cmp,
// Try to optimize things like "A[i] & 42 == 0" to index computations.
Value *X = And->getOperand(0);
Value *Y = And->getOperand(1);
- if (auto *LI = dyn_cast<LoadInst>(X))
- if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
- if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
- if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
- !LI->isVolatile() && isa<ConstantInt>(Y)) {
- ConstantInt *C2 = cast<ConstantInt>(Y);
- if (Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, Cmp, C2))
+ if (auto *C2 = dyn_cast<ConstantInt>(Y))
+ if (auto *LI = dyn_cast<LoadInst>(X))
+ if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
+ if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
+ if (Instruction *Res =
+ foldCmpLoadFromIndexedGlobal(LI, GEP, GV, Cmp, C2))
return Res;
- }
if (!Cmp.isEquality())
return nullptr;
@@ -3476,13 +3478,11 @@ Instruction *InstCombinerImpl::foldICmpInstWithConstantNotInt(ICmpInst &I) {
case Instruction::Load:
// Try to optimize things like "A[i] > 4" to index computations.
if (GetElementPtrInst *GEP =
- dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
+ dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
- if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
- !cast<LoadInst>(LHSI)->isVolatile())
- if (Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, I))
- return Res;
- }
+ if (Instruction *Res =
+ foldCmpLoadFromIndexedGlobal(cast<LoadInst>(LHSI), GEP, GV, I))
+ return Res;
break;
}
@@ -6632,10 +6632,9 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
case Instruction::Load:
if (auto *GEP = dyn_cast<GetElementPtrInst>(LHSI->getOperand(0)))
if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
- if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
- !cast<LoadInst>(LHSI)->isVolatile())
- if (Instruction *Res = foldCmpLoadFromIndexedGlobal(GEP, GV, I))
- return Res;
+ if (Instruction *Res = foldCmpLoadFromIndexedGlobal(
+ cast<LoadInst>(LHSI), GEP, GV, I))
+ return Res;
break;
}
}
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index c776ab6fbc2ec..674d20461daff 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -652,7 +652,8 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
ICmpInst::Predicate Cond, Instruction &I);
Instruction *foldAllocaCmp(ICmpInst &ICI, const AllocaInst *Alloca,
const Value *Other);
- Instruction *foldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
+ Instruction *foldCmpLoadFromIndexedGlobal(LoadInst *LI,
+ GetElementPtrInst *GEP,
GlobalVariable *GV, CmpInst &ICI,
ConstantInt *AndCst = nullptr);
Instruction *foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI,
diff --git a/llvm/test/Transforms/InstCombine/opaque-ptr.ll b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
index be715565bc5de..f054df6244e13 100644
--- a/llvm/test/Transforms/InstCombine/opaque-ptr.ll
+++ b/llvm/test/Transforms/InstCombine/opaque-ptr.ll
@@ -305,3 +305,42 @@ define i1 @cmp_gep_same_base_
diff erent_type(ptr %ptr, i64 %idx1, i64 %idx2) {
%cmp = icmp ult ptr %gep1, %gep2
ret i1 %cmp
}
+
+ at ary = constant [4 x i8] [i8 1, i8 2, i8 3, i8 4]
+
+define i1 @cmp_load_gep_global(i64 %idx) {
+; CHECK-LABEL: @cmp_load_gep_global(
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[IDX:%.*]], 2
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %gep = getelementptr [4 x i8], ptr @ary, i64 0, i64 %idx
+ %load = load i8, ptr %gep
+ %cmp = icmp eq i8 %load, 3
+ ret i1 %cmp
+}
+
+define i1 @cmp_load_gep_global_
diff erent_load_type(i64 %idx) {
+; CHECK-LABEL: @cmp_load_gep_global_
diff erent_load_type(
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr [4 x i8], ptr @ary, i64 0, i64 [[IDX:%.*]]
+; CHECK-NEXT: [[LOAD:%.*]] = load i16, ptr [[GEP]], align 2
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i16 [[LOAD]], 3
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %gep = getelementptr [4 x i8], ptr @ary, i64 0, i64 %idx
+ %load = load i16, ptr %gep
+ %cmp = icmp eq i16 %load, 3
+ ret i1 %cmp
+}
+
+define i1 @cmp_load_gep_global_
diff erent_gep_type(i64 %idx) {
+; CHECK-LABEL: @cmp_load_gep_global_
diff erent_gep_type(
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr [4 x i16], ptr @ary, i64 0, i64 [[IDX:%.*]]
+; CHECK-NEXT: [[LOAD:%.*]] = load i16, ptr [[GEP]], align 2
+; CHECK-NEXT: [[CMP:%.*]] = icmp eq i16 [[LOAD]], 3
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %gep = getelementptr [4 x i16], ptr @ary, i64 0, i64 %idx
+ %load = load i16, ptr %gep
+ %cmp = icmp eq i16 %load, 3
+ ret i1 %cmp
+}
More information about the llvm-commits
mailing list