@@ -105,10 +105,14 @@ static bool isSignTest(ICmpInst::Predicate &Pred, const APInt &C) {
105
105
// /
106
106
// / If AndCst is non-null, then the loaded value is masked with that constant
107
107
// / before doing the comparison. This handles cases like "A[i]&4 == 0".
108
- Instruction *
109
- InstCombinerImpl::foldCmpLoadFromIndexedGlobal (GetElementPtrInst *GEP,
110
- GlobalVariable *GV, CmpInst &ICI,
111
- ConstantInt *AndCst) {
108
+ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal (
109
+ LoadInst *LI, GetElementPtrInst *GEP, GlobalVariable *GV, CmpInst &ICI,
110
+ ConstantInt *AndCst) {
111
+ if (LI->isVolatile () || LI->getType () != GEP->getResultElementType () ||
112
+ GV->getValueType () != GEP->getSourceElementType () ||
113
+ !GV->isConstant () || !GV->hasDefinitiveInitializer ())
114
+ return nullptr ;
115
+
112
116
Constant *Init = GV->getInitializer ();
113
117
if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init))
114
118
return nullptr ;
@@ -1865,15 +1869,13 @@ Instruction *InstCombinerImpl::foldICmpAndConstant(ICmpInst &Cmp,
1865
1869
// Try to optimize things like "A[i] & 42 == 0" to index computations.
1866
1870
Value *X = And->getOperand (0 );
1867
1871
Value *Y = And->getOperand (1 );
1868
- if (auto *LI = dyn_cast<LoadInst>(X))
1869
- if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand (0 )))
1870
- if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand (0 )))
1871
- if (GV->isConstant () && GV->hasDefinitiveInitializer () &&
1872
- !LI->isVolatile () && isa<ConstantInt>(Y)) {
1873
- ConstantInt *C2 = cast<ConstantInt>(Y);
1874
- if (Instruction *Res = foldCmpLoadFromIndexedGlobal (GEP, GV, Cmp, C2))
1872
+ if (auto *C2 = dyn_cast<ConstantInt>(Y))
1873
+ if (auto *LI = dyn_cast<LoadInst>(X))
1874
+ if (auto *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand (0 )))
1875
+ if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand (0 )))
1876
+ if (Instruction *Res =
1877
+ foldCmpLoadFromIndexedGlobal (LI, GEP, GV, Cmp, C2))
1875
1878
return Res;
1876
- }
1877
1879
1878
1880
if (!Cmp.isEquality ())
1879
1881
return nullptr ;
@@ -3476,13 +3478,11 @@ Instruction *InstCombinerImpl::foldICmpInstWithConstantNotInt(ICmpInst &I) {
3476
3478
case Instruction::Load:
3477
3479
// Try to optimize things like "A[i] > 4" to index computations.
3478
3480
if (GetElementPtrInst *GEP =
3479
- dyn_cast<GetElementPtrInst>(LHSI->getOperand (0 ))) {
3481
+ dyn_cast<GetElementPtrInst>(LHSI->getOperand (0 )))
3480
3482
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand (0 )))
3481
- if (GV->isConstant () && GV->hasDefinitiveInitializer () &&
3482
- !cast<LoadInst>(LHSI)->isVolatile ())
3483
- if (Instruction *Res = foldCmpLoadFromIndexedGlobal (GEP, GV, I))
3484
- return Res;
3485
- }
3483
+ if (Instruction *Res =
3484
+ foldCmpLoadFromIndexedGlobal (cast<LoadInst>(LHSI), GEP, GV, I))
3485
+ return Res;
3486
3486
break ;
3487
3487
}
3488
3488
@@ -6632,10 +6632,9 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
6632
6632
case Instruction::Load:
6633
6633
if (auto *GEP = dyn_cast<GetElementPtrInst>(LHSI->getOperand (0 )))
6634
6634
if (auto *GV = dyn_cast<GlobalVariable>(GEP->getOperand (0 )))
6635
- if (GV->isConstant () && GV->hasDefinitiveInitializer () &&
6636
- !cast<LoadInst>(LHSI)->isVolatile ())
6637
- if (Instruction *Res = foldCmpLoadFromIndexedGlobal (GEP, GV, I))
6638
- return Res;
6635
+ if (Instruction *Res = foldCmpLoadFromIndexedGlobal (
6636
+ cast<LoadInst>(LHSI), GEP, GV, I))
6637
+ return Res;
6639
6638
break ;
6640
6639
}
6641
6640
}
0 commit comments