@@ -28,6 +28,13 @@ Value *llvm::emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL,
28
28
// If the GEP is inbounds, we know that none of the addressing operations will
29
29
// overflow in a signed sense.
30
30
bool isInBounds = GEPOp->isInBounds () && !NoAssumptions;
31
+ auto AddOffset = [&](Value *Offset) {
32
+ if (Result)
33
+ Result = Builder->CreateAdd (Result, Offset, GEP->getName () + " .offs" ,
34
+ false /* NUW*/ , isInBounds /* NSW*/ );
35
+ else
36
+ Result = Offset;
37
+ };
31
38
32
39
// Build a mask for high order bits.
33
40
unsigned IntPtrWidth = IntIdxTy->getScalarType ()->getIntegerBitWidth ();
@@ -39,7 +46,6 @@ Value *llvm::emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL,
39
46
++i, ++GTI) {
40
47
Value *Op = *i;
41
48
uint64_t Size = DL.getTypeAllocSize (GTI.getIndexedType ()) & PtrSizeMask;
42
- Value *Offset;
43
49
if (Constant *OpC = dyn_cast<Constant>(Op)) {
44
50
if (OpC->isZeroValue ())
45
51
continue ;
@@ -51,42 +57,26 @@ Value *llvm::emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL,
51
57
if (!Size)
52
58
continue ;
53
59
54
- Offset = ConstantInt::get (IntIdxTy, Size);
55
- } else {
56
- // Splat the constant if needed.
57
- if (IntIdxTy->isVectorTy () && !OpC->getType ()->isVectorTy ())
58
- OpC = ConstantVector::getSplat (
59
- cast<VectorType>(IntIdxTy)->getElementCount (), OpC);
60
-
61
- Constant *Scale = ConstantInt::get (IntIdxTy, Size);
62
- Constant *OC =
63
- ConstantExpr::getIntegerCast (OpC, IntIdxTy, true /* SExt*/ );
64
- Offset =
65
- ConstantExpr::getMul (OC, Scale, false /* NUW*/ , isInBounds /* NSW*/ );
66
- }
67
- } else {
68
- // Splat the index if needed.
69
- if (IntIdxTy->isVectorTy () && !Op->getType ()->isVectorTy ())
70
- Op = Builder->CreateVectorSplat (
71
- cast<FixedVectorType>(IntIdxTy)->getNumElements (), Op);
72
-
73
- // Convert to correct type.
74
- if (Op->getType () != IntIdxTy)
75
- Op = Builder->CreateIntCast (Op, IntIdxTy, true , Op->getName () + " .c" );
76
- if (Size != 1 ) {
77
- // We'll let instcombine(mul) convert this to a shl if possible.
78
- Op = Builder->CreateMul (Op, ConstantInt::get (IntIdxTy, Size),
79
- GEP->getName () + " .idx" , false /* NUW*/ ,
80
- isInBounds /* NSW*/ );
60
+ AddOffset (ConstantInt::get (IntIdxTy, Size));
61
+ continue ;
81
62
}
82
- Offset = Op;
83
63
}
84
64
85
- if (Result)
86
- Result = Builder->CreateAdd (Result, Offset, GEP->getName () + " .offs" ,
87
- false /* NUW*/ , isInBounds /* NSW*/ );
88
- else
89
- Result = Offset;
65
+ // Splat the index if needed.
66
+ if (IntIdxTy->isVectorTy () && !Op->getType ()->isVectorTy ())
67
+ Op = Builder->CreateVectorSplat (
68
+ cast<FixedVectorType>(IntIdxTy)->getNumElements (), Op);
69
+
70
+ // Convert to correct type.
71
+ if (Op->getType () != IntIdxTy)
72
+ Op = Builder->CreateIntCast (Op, IntIdxTy, true , Op->getName () + " .c" );
73
+ if (Size != 1 ) {
74
+ // We'll let instcombine(mul) convert this to a shl if possible.
75
+ Op = Builder->CreateMul (Op, ConstantInt::get (IntIdxTy, Size),
76
+ GEP->getName () + " .idx" , false /* NUW*/ ,
77
+ isInBounds /* NSW*/ );
78
+ }
79
+ AddOffset (Op);
90
80
}
91
81
return Result ? Result : Constant::getNullValue (IntIdxTy);
92
82
}
0 commit comments