Skip to content

Commit 2672719

Browse files
authored
[InstCombine] Don't handle non-canonical index type in icmp of load fold (#151346)
We should just bail out and wait for it to be canonicalized. The current implementation could emit a trunc without actually performing the transform.
1 parent 66b34bc commit 2672719

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(
163163
LaterIndices.push_back(IdxVal);
164164
}
165165

166+
Value *Idx = GEP->getOperand(2);
167+
// If the index type is non-canonical, wait for it to be canonicalized.
168+
if (Idx->getType() != DL.getIndexType(GEP->getType()))
169+
return nullptr;
170+
166171
enum { Overdefined = -3, Undefined = -2 };
167172

168173
// Variables for our state machines.
@@ -290,17 +295,6 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(
290295

291296
// Now that we've scanned the entire array, emit our new comparison(s). We
292297
// order the state machines in complexity of the generated code.
293-
Value *Idx = GEP->getOperand(2);
294-
295-
// If the index is larger than the pointer offset size of the target, truncate
296-
// the index down like the GEP would do implicitly. We don't have to do this
297-
// for an inbounds GEP because the index can't be out of range.
298-
if (!GEP->isInBounds()) {
299-
Type *PtrIdxTy = DL.getIndexType(GEP->getType());
300-
unsigned OffsetSize = PtrIdxTy->getIntegerBitWidth();
301-
if (Idx->getType()->getPrimitiveSizeInBits().getFixedValue() > OffsetSize)
302-
Idx = Builder.CreateTrunc(Idx, PtrIdxTy);
303-
}
304298

305299
// If inbounds keyword is not present, Idx * ElementSize can overflow.
306300
// Let's assume that ElementSize is 2 and the wanted value is at offset 0.

0 commit comments

Comments
 (0)