-
- } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
- Value *Pointer = LI->getPtrOperand();
-
- // Peephole optimize the following instructions:
- // %t1 = getelementptr {<...>} * %StructPtr, <element indices>
- // %V = load <elementty> * %t1
- //
- // Into: load {<...>} * %StructPtr, <element indices>
- //
- if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Pointer)) {
- // Append any indices that the load instruction has onto the end of the
- // ones that the GEP is carrying...
- //
- vector<ConstPoolVal*> Indices(GEP->getIndices());
- Indices.insert(Indices.end(), LI->getIndices().begin(),
- LI->getIndices().end());
-
- PRINT_PEEPHOLE2("gep-load:in", GEP, LI);
- ReplaceInstWithInst(BB->getInstList(), BI,
- LI = new LoadInst(GEP->getPtrOperand(),
- Indices));
- PRINT_PEEPHOLE1("gep-load:out", LI);
- return true;
- }
-
-
- // Peephole optimize the following instructions:
- // %t1 = cast <ty> * %t0 to <ty2> *
- // %V = load <ty2> * %t1
- //
- // Into: %t1 = load <ty> * %t0
- // %V = cast <ty> %t1 to <ty2>
- //
- // The idea behind this transformation is that if the expression type
- // conversion engine could not convert the cast into some other nice form,
- // that there is something fundementally wrong with the current shape of
- // the program. Move the cast through the load and try again. This will
- // leave the original cast instruction, to presumably become dead.
- //
- if (CastInst *CI = dyn_cast<CastInst>(Pointer)) {
- Value *SrcVal = CI->getOperand(0);
- const PointerType *SrcTy = dyn_cast<PointerType>(SrcVal->getType());
- const Type *ElTy = SrcTy ? SrcTy->getValueType() : 0;
-
- // Make sure that nothing will be lost in the new cast...
- if (SrcTy && losslessCastableTypes(ElTy, LI->getType())) {
- PRINT_PEEPHOLE2("CL-LoadCast:in ", CI, LI);
-
- string CName = CI->getName(); CI->setName("");
- LoadInst *NLI = new LoadInst(SrcVal, LI->getName());
- LI->setName(""); // Take over the old load's name
-
- // Insert the load before the old load
- BI = BB->getInstList().insert(BI, NLI)+1;
-
- // Replace the old load with a new cast...
- ReplaceInstWithInst(BB->getInstList(), BI,
- CI = new CastInst(NLI, LI->getType(), CName));
- PRINT_PEEPHOLE2("CL-LoadCast:out", NLI, CI);
-
- return true;
- }
- }