1 //===- ExprTypeConvert.cpp - Code to change an LLVM Expr Type ---------------=//
3 // This file implements the part of level raising that checks to see if it is
4 // possible to coerce an entire expression tree into a different type. If
5 // convertable, other routines from this file will do the conversion.
7 //===----------------------------------------------------------------------===//
9 #include "TransformInternals.h"
10 #include "llvm/Method.h"
11 #include "llvm/Support/STLExtras.h"
12 #include "llvm/iOther.h"
13 #include "llvm/iMemory.h"
14 #include "llvm/ConstPoolVals.h"
15 #include "llvm/Optimizations/ConstantHandling.h"
16 #include "llvm/Optimizations/DCE.h"
20 #include "llvm/Assembly/Writer.h"
22 //#define DEBUG_EXPR_CONVERT 1
24 static inline const Type *getTy(const Value *V, ValueTypeCache &CT) {
25 ValueTypeCache::iterator I = CT.find(V);
26 if (I == CT.end()) return V->getType();
30 GetElementPtrInst *getAddToGEPResult(const Type *Ty, const Value *V) {
31 const StructType *StructTy = getPointedToStruct(Ty);
32 if (StructTy == 0) return 0; // Must be a pointer to a struct...
34 // Must be a constant unsigned offset value... get it now...
35 if (!isa<ConstPoolUInt>(V)) return 0;
36 unsigned Offset = cast<ConstPoolUInt>(V)->getValue();
38 // Check to make sure the offset is somewhat legitiment w.r.t the struct
40 if (Offset >= TD.getTypeSize(StructTy)) return 0;
42 // If we get this far, we have succeeded... TODO: We need to handle array
43 // indexing as well...
44 const StructLayout *SL = TD.getStructLayout(StructTy);
45 vector<ConstPoolVal*> Offsets;
46 unsigned ActualOffset = Offset;
47 const Type *ElTy = getStructOffsetType(StructTy, ActualOffset, Offsets);
49 if (ActualOffset != Offset) return 0; // TODO: Handle Array indexing...
51 // Success! Return the GEP instruction, with a dummy first argument.
52 ConstPoolVal *Dummy = ConstPoolVal::getNullConstant(Ty);
53 return new GetElementPtrInst(Dummy, Offsets);
58 static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
59 ValueTypeCache &ConvertedTypes);
61 static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
65 // ExpressionConvertableToType - Return true if it is possible
66 bool ExpressionConvertableToType(Value *V, const Type *Ty,
67 ValueTypeCache &CTMap) {
68 // Expression type must be holdable in a register.
69 if (!isFirstClassType(Ty))
72 ValueTypeCache::iterator CTMI = CTMap.find(V);
73 if (CTMI != CTMap.end()) return CTMI->second == Ty;
76 // Expressions are only convertable if all of the users of the expression can
77 // have this value converted. This makes use of the map to avoid infinite
80 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I)
81 if (!OperandConvertableToType(*I, V, Ty, CTMap))
84 Instruction *I = dyn_cast<Instruction>(V);
86 // It's not an instruction, check to see if it's a constant... all constants
87 // can be converted to an equivalent value (except pointers, they can't be
88 // const prop'd in general). We just ask the constant propogator to see if
89 // it can convert the value...
91 if (ConstPoolVal *CPV = dyn_cast<ConstPoolVal>(V))
92 if (opt::ConstantFoldCastInstruction(CPV, Ty))
93 return true; // Don't worry about deallocating, it's a constant.
95 return false; // Otherwise, we can't convert!
97 if (I->getType() == Ty) return false; // Expression already correct type!
99 switch (I->getOpcode()) {
100 case Instruction::Cast:
101 // We can convert the expr if the cast destination type is losslessly
102 // convertable to the requested type.
103 return losslessCastableTypes(Ty, I->getType());
105 case Instruction::Add:
106 case Instruction::Sub:
107 return ExpressionConvertableToType(I->getOperand(0), Ty, CTMap) &&
108 ExpressionConvertableToType(I->getOperand(1), Ty, CTMap);
109 case Instruction::Shr:
110 if (Ty->isSigned() != V->getType()->isSigned()) return false;
112 case Instruction::Shl:
113 return ExpressionConvertableToType(I->getOperand(0), Ty, CTMap);
115 case Instruction::Load: {
116 LoadInst *LI = cast<LoadInst>(I);
117 if (LI->hasIndices()) return false;
119 return ExpressionConvertableToType(LI->getPtrOperand(),
120 PointerType::get(Ty), CTMap);
122 case Instruction::PHINode: {
123 PHINode *PN = cast<PHINode>(I);
124 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i)
125 if (!ExpressionConvertableToType(PN->getIncomingValue(i), Ty, CTMap))
130 case Instruction::GetElementPtr: {
131 // GetElementPtr's are directly convertable to a pointer type if they have
132 // a number of zeros at the end. Because removing these values does not
133 // change the logical offset of the GEP, it is okay and fair to remove them.
134 // This can change this:
135 // %t1 = getelementptr %Hosp * %hosp, ubyte 4, ubyte 0 ; <%List **>
136 // %t2 = cast %List * * %t1 to %List *
138 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *>
140 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
141 const PointerType *PTy = dyn_cast<PointerType>(Ty);
142 if (!PTy) return false;
144 // Check to see if there are zero elements that we can remove from the
145 // index array. If there are, check to see if removing them causes us to
146 // get to the right type...
148 vector<ConstPoolVal*> Indices = GEP->getIndices();
149 const Type *BaseType = GEP->getPtrOperand()->getType();
151 while (Indices.size() &&
152 cast<ConstPoolUInt>(Indices.back())->getValue() == 0) {
154 const Type *ElTy = GetElementPtrInst::getIndexedType(BaseType, Indices,
156 if (ElTy == PTy->getValueType())
157 return true; // Found a match!!
159 break; // No match, maybe next time.
168 Value *ConvertExpressionToType(Value *V, const Type *Ty, ValueMapCache &VMC) {
169 ValueMapCache::ExprMapTy::iterator VMCI = VMC.ExprMap.find(V);
170 if (VMCI != VMC.ExprMap.end())
173 #ifdef DEBUG_EXPR_CONVERT
174 cerr << "CETT: " << (void*)V << " " << V;
177 Instruction *I = dyn_cast<Instruction>(V);
179 if (ConstPoolVal *CPV = cast<ConstPoolVal>(V)) {
180 // Constants are converted by constant folding the cast that is required.
181 // We assume here that all casts are implemented for constant prop.
182 Value *Result = opt::ConstantFoldCastInstruction(CPV, Ty);
183 assert(Result && "ConstantFoldCastInstruction Failed!!!");
185 // Add the instruction to the expression map
186 VMC.ExprMap[V] = Result;
191 BasicBlock *BB = I->getParent();
192 BasicBlock::InstListType &BIL = BB->getInstList();
193 string Name = I->getName(); if (!Name.empty()) I->setName("");
194 Instruction *Res; // Result of conversion
196 ValueHandle IHandle(I); // Prevent I from being removed!
198 ConstPoolVal *Dummy = ConstPoolVal::getNullConstant(Ty);
200 //cerr << endl << endl << "Type:\t" << Ty << "\nInst: " << I << "BB Before: " << BB << endl;
202 switch (I->getOpcode()) {
203 case Instruction::Cast:
204 Res = new CastInst(I->getOperand(0), Ty, Name);
207 case Instruction::Add:
208 case Instruction::Sub:
209 Res = BinaryOperator::create(cast<BinaryOperator>(I)->getOpcode(),
211 VMC.ExprMap[I] = Res; // Add node to expression eagerly
213 Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), Ty, VMC));
214 Res->setOperand(1, ConvertExpressionToType(I->getOperand(1), Ty, VMC));
217 case Instruction::Shl:
218 case Instruction::Shr:
219 Res = new ShiftInst(cast<ShiftInst>(I)->getOpcode(), Dummy,
220 I->getOperand(1), Name);
221 VMC.ExprMap[I] = Res;
222 Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), Ty, VMC));
225 case Instruction::Load: {
226 LoadInst *LI = cast<LoadInst>(I);
227 assert(!LI->hasIndices());
228 Res = new LoadInst(ConstPoolVal::getNullConstant(PointerType::get(Ty)),
230 VMC.ExprMap[I] = Res;
231 Res->setOperand(0, ConvertExpressionToType(LI->getPtrOperand(),
232 PointerType::get(Ty), VMC));
236 case Instruction::PHINode: {
237 PHINode *OldPN = cast<PHINode>(I);
238 PHINode *NewPN = new PHINode(Ty, Name);
240 VMC.ExprMap[I] = NewPN; // Add node to expression eagerly
241 while (OldPN->getNumOperands()) {
242 BasicBlock *BB = OldPN->getIncomingBlock(0);
243 Value *OldVal = OldPN->getIncomingValue(0);
244 ValueHandle OldValHandle(OldVal);
245 OldPN->removeIncomingValue(BB);
246 Value *V = ConvertExpressionToType(OldVal, Ty, VMC);
247 NewPN->addIncoming(V, BB);
253 case Instruction::GetElementPtr: {
254 // GetElementPtr's are directly convertable to a pointer type if they have
255 // a number of zeros at the end. Because removing these values does not
256 // change the logical offset of the GEP, it is okay and fair to remove them.
257 // This can change this:
258 // %t1 = getelementptr %Hosp * %hosp, ubyte 4, ubyte 0 ; <%List **>
259 // %t2 = cast %List * * %t1 to %List *
261 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *>
263 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
265 // Check to see if there are zero elements that we can remove from the
266 // index array. If there are, check to see if removing them causes us to
267 // get to the right type...
269 vector<ConstPoolVal*> Indices = GEP->getIndices();
270 const Type *BaseType = GEP->getPtrOperand()->getType();
271 const Type *PVTy = cast<PointerType>(Ty)->getValueType();
273 while (Indices.size() &&
274 cast<ConstPoolUInt>(Indices.back())->getValue() == 0) {
276 if (GetElementPtrInst::getIndexedType(BaseType, Indices, true) == PVTy) {
277 if (Indices.size() == 0) {
278 Res = new CastInst(GEP->getPtrOperand(), BaseType); // NOOP
280 Res = new GetElementPtrInst(GEP->getPtrOperand(), Indices, Name);
285 assert(Res && "Didn't find match!");
286 break; // No match, maybe next time.
290 assert(0 && "Expression convertable, but don't know how to convert?");
294 BasicBlock::iterator It = find(BIL.begin(), BIL.end(), I);
295 assert(It != BIL.end() && "Instruction not in own basic block??");
298 // Add the instruction to the expression map
299 VMC.ExprMap[I] = Res;
301 // Expressions are only convertable if all of the users of the expression can
302 // have this value converted. This makes use of the map to avoid infinite
305 unsigned NumUses = I->use_size();
306 for (unsigned It = 0; It < NumUses; ) {
307 unsigned OldSize = NumUses;
308 ConvertOperandToType(*(I->use_begin()+It), I, Res, VMC);
309 NumUses = I->use_size();
310 if (NumUses == OldSize) ++It;
313 #ifdef DEBUG_EXPR_CONVERT
314 cerr << "ExpIn: " << (void*)I << " " << I
315 << "ExpOut: " << (void*)Res << " " << Res;
316 cerr << "ExpCREATED: " << (void*)Res << " " << Res;
319 if (I->use_empty()) {
320 #ifdef DEBUG_EXPR_CONVERT
321 cerr << "EXPR DELETING: " << (void*)I << " " << I;
332 // RetValConvertableToType - Return true if it is possible
333 bool RetValConvertableToType(Value *V, const Type *Ty,
334 ValueTypeCache &ConvertedTypes) {
335 ValueTypeCache::iterator I = ConvertedTypes.find(V);
336 if (I != ConvertedTypes.end()) return I->second == Ty;
337 ConvertedTypes[V] = Ty;
339 // It is safe to convert the specified value to the specified type IFF all of
340 // the uses of the value can be converted to accept the new typed value.
342 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I)
343 if (!OperandConvertableToType(*I, V, Ty, ConvertedTypes))
355 // OperandConvertableToType - Return true if it is possible to convert operand
356 // V of User (instruction) U to the specified type. This is true iff it is
357 // possible to change the specified instruction to accept this. CTMap is a map
358 // of converted types, so that circular definitions will see the future type of
359 // the expression, not the static current type.
361 static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
362 ValueTypeCache &CTMap) {
363 if (V->getType() == Ty) return true; // Already the right type?
365 // Expression type must be holdable in a register.
366 if (!isFirstClassType(Ty))
369 Instruction *I = dyn_cast<Instruction>(U);
370 if (I == 0) return false; // We can't convert!
372 switch (I->getOpcode()) {
373 case Instruction::Cast:
374 assert(I->getOperand(0) == V);
375 // We can convert the expr if the cast destination type is losslessly
376 // convertable to the requested type.
377 return losslessCastableTypes(Ty, I->getOperand(0)->getType());
379 case Instruction::Add:
380 if (V == I->getOperand(0) && isa<CastInst>(I->getOperand(1))) {
382 getAddToGEPResult(Ty, cast<CastInst>(I->getOperand(1))->getOperand(0));
383 if (GEP) { // If successful, this Add can be converted to a GEP.
384 const Type *RetTy = GEP->getType(); // Get the new type...
385 delete GEP; // We don't want the actual instruction yet...
386 // Only successful if we can convert this type to the required type
387 return RetValConvertableToType(I, RetTy, CTMap);
391 case Instruction::Sub: {
392 Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0);
393 return RetValConvertableToType(I, Ty, CTMap) &&
394 ExpressionConvertableToType(OtherOp, Ty, CTMap);
396 case Instruction::SetEQ:
397 case Instruction::SetNE: {
398 Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0);
399 return ExpressionConvertableToType(OtherOp, Ty, CTMap);
401 case Instruction::Shr:
402 if (Ty->isSigned() != V->getType()->isSigned()) return false;
404 case Instruction::Shl:
405 assert(I->getOperand(0) == V);
406 return RetValConvertableToType(I, Ty, CTMap);
408 case Instruction::Load:
409 assert(I->getOperand(0) == V);
410 if (const PointerType *PT = dyn_cast<PointerType>(Ty)) {
411 LoadInst *LI = cast<LoadInst>(I);
412 const Type *PVTy = PT->getValueType();
414 if (LI->hasIndices() || isa<ArrayType>(PVTy))
417 if (!isFirstClassType(PVTy)) {
418 // They could be loading the first element of a structure type...
419 if (const StructType *ST = dyn_cast<StructType>(PVTy)) {
420 unsigned Offset = 0; // No offset, get first leaf.
421 vector<ConstPoolVal*> Offsets; // Discarded...
422 const Type *Ty = getStructOffsetType(ST, Offset, Offsets, false);
423 assert(Offset == 0 && "Offset changed from zero???");
424 if (!isFirstClassType(Ty)) return false;
426 // See if the leaf type is compatible with the old return type...
427 if (TD.getTypeSize(Ty) != TD.getTypeSize(LI->getType()))
430 return RetValConvertableToType(LI, Ty, CTMap);
435 if (TD.getTypeSize(PVTy) != TD.getTypeSize(LI->getType()))
438 return RetValConvertableToType(LI, PVTy, CTMap);
442 case Instruction::Store: {
443 StoreInst *SI = cast<StoreInst>(I);
444 if (SI->hasIndices()) return false;
446 if (V == I->getOperand(0)) {
447 // Can convert the store if we can convert the pointer operand to match
448 // the new value type...
449 return ExpressionConvertableToType(I->getOperand(1), PointerType::get(Ty),
451 } else if (const PointerType *PT = dyn_cast<PointerType>(Ty)) {
452 if (isa<ArrayType>(PT->getValueType()))
453 return false; // Avoid getDataSize on unsized array type!
454 assert(V == I->getOperand(1));
456 // Must move the same amount of data...
457 if (TD.getTypeSize(PT->getValueType()) !=
458 TD.getTypeSize(I->getOperand(0)->getType())) return false;
460 // Can convert store if the incoming value is convertable...
461 return ExpressionConvertableToType(I->getOperand(0), PT->getValueType(),
467 case Instruction::PHINode: {
468 PHINode *PN = cast<PHINode>(I);
469 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i)
470 if (!ExpressionConvertableToType(PN->getIncomingValue(i), Ty, CTMap))
472 return RetValConvertableToType(PN, Ty, CTMap);
476 case Instruction::GetElementPtr: {
477 // GetElementPtr's are directly convertable to a pointer type if they have
478 // a number of zeros at the end. Because removing these values does not
479 // change the logical offset of the GEP, it is okay and fair to remove them.
480 // This can change this:
481 // %t1 = getelementptr %Hosp * %hosp, ubyte 4, ubyte 0 ; <%List **>
482 // %t2 = cast %List * * %t1 to %List *
484 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *>
486 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
487 const PointerType *PTy = dyn_cast<PointerType>(Ty);
488 if (!PTy) return false;
490 // Check to see if there are zero elements that we can remove from the
491 // index array. If there are, check to see if removing them causes us to
492 // get to the right type...
494 vector<ConstPoolVal*> Indices = GEP->getIndices();
495 const Type *BaseType = GEP->getPtrOperand()->getType();
497 while (Indices.size() &&
498 cast<ConstPoolUInt>(Indices.back())->getValue() == 0) {
500 const Type *ElTy = GetElementPtrInst::getIndexedType(BaseType, Indices,
502 if (ElTy == PTy->getValueType())
503 return true; // Found a match!!
505 break; // No match, maybe next time.
513 void ConvertUsersType(Value *V, Value *NewVal, ValueMapCache &VMC) {
516 unsigned NumUses = V->use_size();
517 for (unsigned It = 0; It < NumUses; ) {
518 unsigned OldSize = NumUses;
519 ConvertOperandToType(*(V->use_begin()+It), V, NewVal, VMC);
520 NumUses = V->use_size();
521 if (NumUses == OldSize) ++It;
527 static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
528 ValueMapCache &VMC) {
529 if (isa<ValueHandle>(U)) return; // Valuehandles don't let go of operands...
531 if (VMC.OperandsMapped.count(U)) return;
532 VMC.OperandsMapped.insert(U);
534 ValueMapCache::ExprMapTy::iterator VMCI = VMC.ExprMap.find(U);
535 if (VMCI != VMC.ExprMap.end())
539 Instruction *I = cast<Instruction>(U); // Only Instructions convertable
541 BasicBlock *BB = I->getParent();
542 BasicBlock::InstListType &BIL = BB->getInstList();
543 string Name = I->getName(); if (!Name.empty()) I->setName("");
544 Instruction *Res; // Result of conversion
546 //cerr << endl << endl << "Type:\t" << Ty << "\nInst: " << I << "BB Before: " << BB << endl;
548 // Prevent I from being removed...
549 ValueHandle IHandle(I);
551 const Type *NewTy = NewVal->getType();
552 ConstPoolVal *Dummy = (NewTy != Type::VoidTy) ?
553 ConstPoolVal::getNullConstant(NewTy) : 0;
555 switch (I->getOpcode()) {
556 case Instruction::Cast:
557 assert(I->getOperand(0) == OldVal);
558 Res = new CastInst(NewVal, I->getType(), Name);
561 case Instruction::Add:
562 if (OldVal == I->getOperand(0) && isa<CastInst>(I->getOperand(1))) {
563 Res = getAddToGEPResult(NewVal->getType(),
564 cast<CastInst>(I->getOperand(1))->getOperand(0));
565 if (Res) { // If successful, this Add should be converted to a GEP.
566 // First operand is actually the given pointer...
567 Res->setOperand(0, NewVal);
573 case Instruction::Sub:
574 case Instruction::SetEQ:
575 case Instruction::SetNE: {
576 Res = BinaryOperator::create(cast<BinaryOperator>(I)->getOpcode(),
578 VMC.ExprMap[I] = Res; // Add node to expression eagerly
580 unsigned OtherIdx = (OldVal == I->getOperand(0)) ? 1 : 0;
581 Value *OtherOp = I->getOperand(OtherIdx);
582 Value *NewOther = ConvertExpressionToType(OtherOp, NewTy, VMC);
584 Res->setOperand(OtherIdx, NewOther);
585 Res->setOperand(!OtherIdx, NewVal);
588 case Instruction::Shl:
589 case Instruction::Shr:
590 assert(I->getOperand(0) == OldVal);
591 Res = new ShiftInst(cast<ShiftInst>(I)->getOpcode(), NewVal,
592 I->getOperand(1), Name);
595 case Instruction::Load: {
596 assert(I->getOperand(0) == OldVal && isa<PointerType>(NewVal->getType()));
597 const Type *PVTy = cast<PointerType>(NewVal->getType())->getValueType();
598 if (!isFirstClassType(PVTy)) { // Must be an indirect load then...
599 assert(isa<StructType>(PVTy));
600 unsigned Offset = 0; // No offset, get first leaf.
601 vector<ConstPoolVal*> Offsets; // Discarded...
602 const Type *Ty = getStructOffsetType(PVTy, Offset, Offsets, false);
603 Res = new LoadInst(NewVal, Offsets, Name);
605 Res = new LoadInst(NewVal, Name);
607 assert(isFirstClassType(Res->getType()) && "Load of structure or array!");
610 case Instruction::Store: {
611 if (I->getOperand(0) == OldVal) { // Replace the source value
612 const PointerType *NewPT = PointerType::get(NewTy);
613 Res = new StoreInst(NewVal, ConstPoolVal::getNullConstant(NewPT));
614 VMC.ExprMap[I] = Res;
615 Res->setOperand(1, ConvertExpressionToType(I->getOperand(1), NewPT, VMC));
616 } else { // Replace the source pointer
617 const Type *ValTy = cast<PointerType>(NewTy)->getValueType();
618 Res = new StoreInst(ConstPoolVal::getNullConstant(ValTy), NewVal);
619 VMC.ExprMap[I] = Res;
620 Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), ValTy, VMC));
625 case Instruction::PHINode: {
626 PHINode *OldPN = cast<PHINode>(I);
627 PHINode *NewPN = new PHINode(NewTy, Name);
628 VMC.ExprMap[I] = NewPN;
630 while (OldPN->getNumOperands()) {
631 BasicBlock *BB = OldPN->getIncomingBlock(0);
632 Value *OldVal = OldPN->getIncomingValue(0);
633 OldPN->removeIncomingValue(BB);
634 Value *V = ConvertExpressionToType(OldVal, NewTy, VMC);
635 NewPN->addIncoming(V, BB);
642 case Instruction::GetElementPtr: {
643 // GetElementPtr's are directly convertable to a pointer type if they have
644 // a number of zeros at the end. Because removing these values does not
645 // change the logical offset of the GEP, it is okay and fair to remove them.
646 // This can change this:
647 // %t1 = getelementptr %Hosp * %hosp, ubyte 4, ubyte 0 ; <%List **>
648 // %t2 = cast %List * * %t1 to %List *
650 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *>
652 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
654 // Check to see if there are zero elements that we can remove from the
655 // index array. If there are, check to see if removing them causes us to
656 // get to the right type...
658 vector<ConstPoolVal*> Indices = GEP->getIndices();
659 const Type *BaseType = GEP->getPtrOperand()->getType();
660 const Type *PVTy = cast<PointerType>(Ty)->getValueType();
662 while (Indices.size() &&
663 cast<ConstPoolUInt>(Indices.back())->getValue() == 0) {
665 if (GetElementPtrInst::getIndexedType(BaseType, Indices, true) == PVTy) {
666 if (Indices.size() == 0) {
667 Res = new CastInst(GEP->getPtrOperand(), BaseType); // NOOP
669 Res = new GetElementPtrInst(GEP->getPtrOperand(), Indices, Name);
674 assert(Res && "Didn't find match!");
675 break; // No match, maybe next time.
680 assert(0 && "Expression convertable, but don't know how to convert?");
684 BasicBlock::iterator It = find(BIL.begin(), BIL.end(), I);
685 assert(It != BIL.end() && "Instruction not in own basic block??");
686 BIL.insert(It, Res); // Keep It pointing to old instruction
688 #ifdef DEBUG_EXPR_CONVERT
689 cerr << "COT CREATED: " << (void*)Res << " " << Res;
690 cerr << "In: " << (void*)I << " " << I << "Out: " << (void*)Res << " " << Res;
693 // Add the instruction to the expression map
694 VMC.ExprMap[I] = Res;
696 if (I->getType() != Res->getType())
697 ConvertUsersType(I, Res, VMC);
699 for (unsigned It = 0; It < I->use_size(); ) {
700 User *Use = *(I->use_begin()+It);
701 if (isa<ValueHandle>(Use)) // Don't remove ValueHandles!
704 Use->replaceUsesOfWith(I, Res);
707 if (I->use_empty()) {
708 // Now we just need to remove the old instruction so we don't get infinite
709 // loops. Note that we cannot use DCE because DCE won't remove a store
710 // instruction, for example.
712 #ifdef DEBUG_EXPR_CONVERT
713 cerr << "DELETING: " << (void*)I << " " << I;
718 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
720 assert(isa<ValueHandle>((Value*)*UI) && "Uses of Instruction remain!!!");
725 ValueHandle::ValueHandle(Value *V) : Instruction(Type::VoidTy, UserOp1, "") {
726 #ifdef DEBUG_EXPR_CONVERT
727 cerr << "VH AQUIRING: " << (void*)V << " " << V;
729 Operands.push_back(Use(V, this));
732 static void RecursiveDelete(Instruction *I) {
733 if (!I || !I->use_empty()) return;
735 assert(I->getParent() && "Inst not in basic block!");
737 #ifdef DEBUG_EXPR_CONVERT
738 cerr << "VH DELETING: " << (void*)I << " " << I;
741 for (User::op_iterator OI = I->op_begin(), OE = I->op_end();
743 Instruction *U = dyn_cast<Instruction>(*OI);
746 RecursiveDelete(dyn_cast<Instruction>(U));
750 I->getParent()->getInstList().remove(I);
755 ValueHandle::~ValueHandle() {
756 if (Operands[0]->use_size() == 1) {
757 Value *V = Operands[0];
758 Operands[0] = 0; // Drop use!
760 // Now we just need to remove the old instruction so we don't get infinite
761 // loops. Note that we cannot use DCE because DCE won't remove a store
762 // instruction, for example.
764 RecursiveDelete(dyn_cast<Instruction>(V));
766 #ifdef DEBUG_EXPR_CONVERT
767 cerr << "VH RELEASING: " << (void*)Operands[0].get() << " " << Operands[0]->use_size() << " " << Operands[0];