- if (!InsertCast) return false; // There is no reason to insert a cast!
-
- // Insert a cast!
- const Type *ElTy = ThePtrType->getValueType();
- const PointerType *DestTy = PointerType::get(ArrayType::get(ElTy));
-
- CastResult = new CastInst(V, DestTy);
- BB->getInstList().insert(InsertBefore, CastResult);
- //cerr << "Inserted cast: " << CastResult;
- return true; // Made a change!
-}
-
-
-// DoInsertArrayCasts - Loop over all "incoming" values in the specified method,
-// inserting a cast for pointer values that are used as arrays. For our
-// purposes, an incoming value is considered to be either a value that is
-// either a method parameter, a value created by alloca or malloc, or a value
-// returned from a function call. All casts are kept attached to their original
-// values through the PtrCasts map.
-//
-static bool DoInsertArrayCasts(Method *M, map<Value*, CastInst*> &PtrCasts) {
- assert(!M->isExternal() && "Can't handle external methods!");
-
- // Insert casts for all arguments to the function...
- bool Changed = false;
- BasicBlock *CurBB = M->front();
- BasicBlock::iterator It = CurBB->begin();
- for (Method::ArgumentListType::iterator AI = M->getArgumentList().begin(),
- AE = M->getArgumentList().end(); AI != AE; ++AI) {
- CastInst *TheCast = 0;
- if (DoInsertArrayCast(M, *AI, CurBB, It, TheCast)) {
- It = CurBB->begin(); // We might have just invalidated the iterator!
- Changed = true; // Yes we made a change
- ++It; // Insert next cast AFTER this one...
- }
-
- if (TheCast) // Is there a cast associated with this value?
- PtrCasts[*AI] = TheCast; // Yes, add it to the map...
- }
-
- // TODO: insert casts for alloca, malloc, and function call results. Also,
- // look for pointers that already have casts, to add to the map.
-
- return Changed;
-}
-