- // Check to see if we have bottomed out INSIDE of an array reference..
- //
- if (Offset != ActualOffset) {
- // Insert a cast of the "rest" of the offset to the appropriate
- // pointer type.
- CastInst *OffInst =
- new CastInst(ConstPoolUInt::get(Type::ULongTy,
- Offset-ActualOffset),
- GEP->getType());
- BI = BB->getInstList().insert(BI, OffInst)+1;
-
- // Now insert an ADD to actually adjust the pointer...
- Instruction *AddInst =
- BinaryOperator::create(Instruction::Add, GEP, OffInst);
- BI = BB->getInstList().insert(BI, AddInst)+1;
-
- PRINT_PEEPHOLE2("add-to-gep:out1", OffInst, AddInst);
-
- AddrSrc = AddInst;
- } else if (Scale != 1) {
- // If the scale factor occurs, then this means that there is an index into
- // this element of the array. Check to make sure the scale factor is the
- // same as the size of the datatype that we are dealing with.
- //
- assert(Scale == TD.getTypeSize(AT->getElementType()) &&
- "Scaling by something other than the array element size!!");
-
- // TODO: In the future, we will not want to cast the index and scale to
- // pointer types first. We will want to create a GEP directly here.
-
- // Now we must actually perform the scaling operation to get an
- // appropriate value to add in... but the scale has to be done in the
- // appropriate destination pointer type, so cast the index value now.
- //
- // Cast the base index pointer
- CastInst *IdxValue = new CastInst(Expr.Var, GEP->getType());
- BI = BB->getInstList().insert(BI, IdxValue)+1;
-
- // Case the scale amount as well...
- CastInst *ScaleAmt =
- new CastInst(ConstPoolUInt::get(Type::ULongTy, Scale), GEP->getType());
- BI = BB->getInstList().insert(BI, ScaleAmt)+1;
-
- // Insert the multiply now. Make sure to make the constant the second arg
- Instruction *ScaledVal =
- BinaryOperator::create(Instruction::Mul, IdxValue, ScaleAmt);
- BI = BB->getInstList().insert(BI, ScaledVal)+1;
-
- // Now insert an ADD to actually adjust the pointer...
- Instruction *AddInst =
- BinaryOperator::create(Instruction::Add, GEP, ScaledVal);
- BI = BB->getInstList().insert(BI, AddInst)+1;
-
- PRINT_PEEPHOLE4("add-to-gep:out1", IdxValue, ScaleAmt, ScaledVal,
- AddInst);
- AddrSrc = AddInst;
- }
-
- // Insert a cast of the pointer to array of X to be a pointer to the
- // element of the array.
- //
- // Insert a cast of the "rest" of the offset to the appropriate
- // pointer type.
- CastInst *ACI = new CastInst(AddrSrc, AT->getElementType());
- BI = BB->getInstList().insert(BI, ACI)+1;
- AddrSrc = ACI;
-
- } else {
- assert(Offset == ActualOffset && "GEP to middle of non array!");
- assert(Scale == 1 && "Scale factor for expr that is not an array idx!");
- }
-
- Instruction *NCI = new CastInst(AddrSrc, AddOp1->getType());