LLVM API Documentation
00001 //===- ExprTypeConvert.cpp - Code to change an LLVM Expr Type -------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the part of level raising that checks to see if it is 00011 // possible to coerce an entire expression tree into a different type. If 00012 // convertible, other routines from this file will do the conversion. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #include "TransformInternals.h" 00017 #include "llvm/Constants.h" 00018 #include "llvm/Instructions.h" 00019 #include "llvm/ADT/STLExtras.h" 00020 #include "llvm/Support/Debug.h" 00021 #include <algorithm> 00022 #include <iostream> 00023 using namespace llvm; 00024 00025 static bool OperandConvertibleToType(User *U, Value *V, const Type *Ty, 00026 ValueTypeCache &ConvertedTypes, 00027 const TargetData &TD); 00028 00029 static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, 00030 ValueMapCache &VMC, const TargetData &TD); 00031 00032 00033 // ExpressionConvertibleToType - Return true if it is possible 00034 bool llvm::ExpressionConvertibleToType(Value *V, const Type *Ty, 00035 ValueTypeCache &CTMap, const TargetData &TD) { 00036 // Expression type must be holdable in a register. 00037 if (!Ty->isFirstClassType()) 00038 return false; 00039 00040 ValueTypeCache::iterator CTMI = CTMap.find(V); 00041 if (CTMI != CTMap.end()) return CTMI->second == Ty; 00042 00043 // If it's a constant... all constants can be converted to a different 00044 // type. 00045 // 00046 if (isa<Constant>(V) && !isa<GlobalValue>(V)) 00047 return true; 00048 00049 CTMap[V] = Ty; 00050 if (V->getType() == Ty) return true; // Expression already correct type! 00051 00052 Instruction *I = dyn_cast<Instruction>(V); 00053 if (I == 0) return false; // Otherwise, we can't convert! 00054 00055 switch (I->getOpcode()) { 00056 case Instruction::Cast: 00057 // We can convert the expr if the cast destination type is losslessly 00058 // convertible to the requested type. 00059 if (!Ty->isLosslesslyConvertibleTo(I->getType())) return false; 00060 00061 // We also do not allow conversion of a cast that casts from a ptr to array 00062 // of X to a *X. For example: cast [4 x %List *] * %val to %List * * 00063 // 00064 if (const PointerType *SPT = 00065 dyn_cast<PointerType>(I->getOperand(0)->getType())) 00066 if (const PointerType *DPT = dyn_cast<PointerType>(I->getType())) 00067 if (const ArrayType *AT = dyn_cast<ArrayType>(SPT->getElementType())) 00068 if (AT->getElementType() == DPT->getElementType()) 00069 return false; 00070 break; 00071 00072 case Instruction::Add: 00073 case Instruction::Sub: 00074 if (!Ty->isInteger() && !Ty->isFloatingPoint()) return false; 00075 if (!ExpressionConvertibleToType(I->getOperand(0), Ty, CTMap, TD) || 00076 !ExpressionConvertibleToType(I->getOperand(1), Ty, CTMap, TD)) 00077 return false; 00078 break; 00079 case Instruction::Shr: 00080 if (!Ty->isInteger()) return false; 00081 if (Ty->isSigned() != V->getType()->isSigned()) return false; 00082 // FALL THROUGH 00083 case Instruction::Shl: 00084 if (!Ty->isInteger()) return false; 00085 if (!ExpressionConvertibleToType(I->getOperand(0), Ty, CTMap, TD)) 00086 return false; 00087 break; 00088 00089 case Instruction::Load: { 00090 LoadInst *LI = cast<LoadInst>(I); 00091 if (!ExpressionConvertibleToType(LI->getPointerOperand(), 00092 PointerType::get(Ty), CTMap, TD)) 00093 return false; 00094 break; 00095 } 00096 case Instruction::PHI: { 00097 PHINode *PN = cast<PHINode>(I); 00098 // Be conservative if we find a giant PHI node. 00099 if (PN->getNumIncomingValues() > 32) return false; 00100 00101 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) 00102 if (!ExpressionConvertibleToType(PN->getIncomingValue(i), Ty, CTMap, TD)) 00103 return false; 00104 break; 00105 } 00106 00107 case Instruction::GetElementPtr: { 00108 // GetElementPtr's are directly convertible to a pointer type if they have 00109 // a number of zeros at the end. Because removing these values does not 00110 // change the logical offset of the GEP, it is okay and fair to remove them. 00111 // This can change this: 00112 // %t1 = getelementptr %Hosp * %hosp, ubyte 4, ubyte 0 ; <%List **> 00113 // %t2 = cast %List * * %t1 to %List * 00114 // into 00115 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *> 00116 // 00117 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I); 00118 const PointerType *PTy = dyn_cast<PointerType>(Ty); 00119 if (!PTy) return false; // GEP must always return a pointer... 00120 const Type *PVTy = PTy->getElementType(); 00121 00122 // Check to see if there are zero elements that we can remove from the 00123 // index array. If there are, check to see if removing them causes us to 00124 // get to the right type... 00125 // 00126 std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end()); 00127 const Type *BaseType = GEP->getPointerOperand()->getType(); 00128 const Type *ElTy = 0; 00129 00130 while (!Indices.empty() && 00131 Indices.back() == Constant::getNullValue(Indices.back()->getType())){ 00132 Indices.pop_back(); 00133 ElTy = GetElementPtrInst::getIndexedType(BaseType, Indices, true); 00134 if (ElTy == PVTy) 00135 break; // Found a match!! 00136 ElTy = 0; 00137 } 00138 00139 if (ElTy) break; // Found a number of zeros we can strip off! 00140 00141 // Otherwise, it could be that we have something like this: 00142 // getelementptr [[sbyte] *] * %reg115, long %reg138 ; [sbyte]** 00143 // and want to convert it into something like this: 00144 // getelemenptr [[int] *] * %reg115, long %reg138 ; [int]** 00145 // 00146 if (GEP->getNumOperands() == 2 && 00147 PTy->getElementType()->isSized() && 00148 TD.getTypeSize(PTy->getElementType()) == 00149 TD.getTypeSize(GEP->getType()->getElementType())) { 00150 const PointerType *NewSrcTy = PointerType::get(PVTy); 00151 if (!ExpressionConvertibleToType(I->getOperand(0), NewSrcTy, CTMap, TD)) 00152 return false; 00153 break; 00154 } 00155 00156 return false; // No match, maybe next time. 00157 } 00158 00159 case Instruction::Call: { 00160 if (isa<Function>(I->getOperand(0))) 00161 return false; // Don't even try to change direct calls. 00162 00163 // If this is a function pointer, we can convert the return type if we can 00164 // convert the source function pointer. 00165 // 00166 const PointerType *PT = cast<PointerType>(I->getOperand(0)->getType()); 00167 const FunctionType *FT = cast<FunctionType>(PT->getElementType()); 00168 std::vector<const Type *> ArgTys(FT->param_begin(), FT->param_end()); 00169 const FunctionType *NewTy = 00170 FunctionType::get(Ty, ArgTys, FT->isVarArg()); 00171 if (!ExpressionConvertibleToType(I->getOperand(0), 00172 PointerType::get(NewTy), CTMap, TD)) 00173 return false; 00174 break; 00175 } 00176 default: 00177 return false; 00178 } 00179 00180 // Expressions are only convertible if all of the users of the expression can 00181 // have this value converted. This makes use of the map to avoid infinite 00182 // recursion. 00183 // 00184 for (Value::use_iterator It = I->use_begin(), E = I->use_end(); It != E; ++It) 00185 if (!OperandConvertibleToType(*It, I, Ty, CTMap, TD)) 00186 return false; 00187 00188 return true; 00189 } 00190 00191 00192 Value *llvm::ConvertExpressionToType(Value *V, const Type *Ty, 00193 ValueMapCache &VMC, const TargetData &TD) { 00194 if (V->getType() == Ty) return V; // Already where we need to be? 00195 00196 ValueMapCache::ExprMapTy::iterator VMCI = VMC.ExprMap.find(V); 00197 if (VMCI != VMC.ExprMap.end()) { 00198 const Value *GV = VMCI->second; 00199 const Type *GTy = VMCI->second->getType(); 00200 assert(VMCI->second->getType() == Ty); 00201 00202 if (Instruction *I = dyn_cast<Instruction>(V)) 00203 ValueHandle IHandle(VMC, I); // Remove I if it is unused now! 00204 00205 return VMCI->second; 00206 } 00207 00208 DEBUG(std::cerr << "CETT: " << (void*)V << " " << *V); 00209 00210 Instruction *I = dyn_cast<Instruction>(V); 00211 if (I == 0) { 00212 Constant *CPV = cast<Constant>(V); 00213 // Constants are converted by constant folding the cast that is required. 00214 // We assume here that all casts are implemented for constant prop. 00215 Value *Result = ConstantExpr::getCast(CPV, Ty); 00216 // Add the instruction to the expression map 00217 //VMC.ExprMap[V] = Result; 00218 return Result; 00219 } 00220 00221 00222 BasicBlock *BB = I->getParent(); 00223 std::string Name = I->getName(); if (!Name.empty()) I->setName(""); 00224 Instruction *Res; // Result of conversion 00225 00226 ValueHandle IHandle(VMC, I); // Prevent I from being removed! 00227 00228 Constant *Dummy = Constant::getNullValue(Ty); 00229 00230 switch (I->getOpcode()) { 00231 case Instruction::Cast: 00232 assert(VMC.NewCasts.count(ValueHandle(VMC, I)) == 0); 00233 Res = new CastInst(I->getOperand(0), Ty, Name); 00234 VMC.NewCasts.insert(ValueHandle(VMC, Res)); 00235 break; 00236 00237 case Instruction::Add: 00238 case Instruction::Sub: 00239 Res = BinaryOperator::create(cast<BinaryOperator>(I)->getOpcode(), 00240 Dummy, Dummy, Name); 00241 VMC.ExprMap[I] = Res; // Add node to expression eagerly 00242 00243 Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), Ty, VMC, TD)); 00244 Res->setOperand(1, ConvertExpressionToType(I->getOperand(1), Ty, VMC, TD)); 00245 break; 00246 00247 case Instruction::Shl: 00248 case Instruction::Shr: 00249 Res = new ShiftInst(cast<ShiftInst>(I)->getOpcode(), Dummy, 00250 I->getOperand(1), Name); 00251 VMC.ExprMap[I] = Res; 00252 Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), Ty, VMC, TD)); 00253 break; 00254 00255 case Instruction::Load: { 00256 LoadInst *LI = cast<LoadInst>(I); 00257 00258 Res = new LoadInst(Constant::getNullValue(PointerType::get(Ty)), Name); 00259 VMC.ExprMap[I] = Res; 00260 Res->setOperand(0, ConvertExpressionToType(LI->getPointerOperand(), 00261 PointerType::get(Ty), VMC, TD)); 00262 assert(Res->getOperand(0)->getType() == PointerType::get(Ty)); 00263 assert(Ty == Res->getType()); 00264 assert(Res->getType()->isFirstClassType() && "Load of structure or array!"); 00265 break; 00266 } 00267 00268 case Instruction::PHI: { 00269 PHINode *OldPN = cast<PHINode>(I); 00270 PHINode *NewPN = new PHINode(Ty, Name); 00271 00272 VMC.ExprMap[I] = NewPN; // Add node to expression eagerly 00273 while (OldPN->getNumOperands()) { 00274 BasicBlock *BB = OldPN->getIncomingBlock(0); 00275 Value *OldVal = OldPN->getIncomingValue(0); 00276 ValueHandle OldValHandle(VMC, OldVal); 00277 OldPN->removeIncomingValue(BB, false); 00278 Value *V = ConvertExpressionToType(OldVal, Ty, VMC, TD); 00279 NewPN->addIncoming(V, BB); 00280 } 00281 Res = NewPN; 00282 break; 00283 } 00284 00285 case Instruction::GetElementPtr: { 00286 // GetElementPtr's are directly convertible to a pointer type if they have 00287 // a number of zeros at the end. Because removing these values does not 00288 // change the logical offset of the GEP, it is okay and fair to remove them. 00289 // This can change this: 00290 // %t1 = getelementptr %Hosp * %hosp, ubyte 4, ubyte 0 ; <%List **> 00291 // %t2 = cast %List * * %t1 to %List * 00292 // into 00293 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *> 00294 // 00295 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I); 00296 00297 // Check to see if there are zero elements that we can remove from the 00298 // index array. If there are, check to see if removing them causes us to 00299 // get to the right type... 00300 // 00301 std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end()); 00302 const Type *BaseType = GEP->getPointerOperand()->getType(); 00303 const Type *PVTy = cast<PointerType>(Ty)->getElementType(); 00304 Res = 0; 00305 while (!Indices.empty() && 00306 Indices.back() == Constant::getNullValue(Indices.back()->getType())){ 00307 Indices.pop_back(); 00308 if (GetElementPtrInst::getIndexedType(BaseType, Indices, true) == PVTy) { 00309 if (Indices.size() == 0) 00310 Res = new CastInst(GEP->getPointerOperand(), BaseType); // NOOP CAST 00311 else 00312 Res = new GetElementPtrInst(GEP->getPointerOperand(), Indices, Name); 00313 break; 00314 } 00315 } 00316 00317 // Otherwise, it could be that we have something like this: 00318 // getelementptr [[sbyte] *] * %reg115, uint %reg138 ; [sbyte]** 00319 // and want to convert it into something like this: 00320 // getelemenptr [[int] *] * %reg115, uint %reg138 ; [int]** 00321 // 00322 if (Res == 0) { 00323 const PointerType *NewSrcTy = PointerType::get(PVTy); 00324 std::vector<Value*> Indices(GEP->idx_begin(), GEP->idx_end()); 00325 Res = new GetElementPtrInst(Constant::getNullValue(NewSrcTy), 00326 Indices, Name); 00327 VMC.ExprMap[I] = Res; 00328 Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), 00329 NewSrcTy, VMC, TD)); 00330 } 00331 00332 00333 assert(Res && "Didn't find match!"); 00334 break; 00335 } 00336 00337 case Instruction::Call: { 00338 assert(!isa<Function>(I->getOperand(0))); 00339 00340 // If this is a function pointer, we can convert the return type if we can 00341 // convert the source function pointer. 00342 // 00343 const PointerType *PT = cast<PointerType>(I->getOperand(0)->getType()); 00344 const FunctionType *FT = cast<FunctionType>(PT->getElementType()); 00345 std::vector<const Type *> ArgTys(FT->param_begin(), FT->param_end()); 00346 const FunctionType *NewTy = 00347 FunctionType::get(Ty, ArgTys, FT->isVarArg()); 00348 const PointerType *NewPTy = PointerType::get(NewTy); 00349 if (Ty == Type::VoidTy) 00350 Name = ""; // Make sure not to name calls that now return void! 00351 00352 Res = new CallInst(Constant::getNullValue(NewPTy), 00353 std::vector<Value*>(I->op_begin()+1, I->op_end()), 00354 Name); 00355 if (cast<CallInst>(I)->isTailCall()) 00356 cast<CallInst>(Res)->setTailCall(); 00357 cast<CallInst>(Res)->setCallingConv(cast<CallInst>(I)->getCallingConv()); 00358 VMC.ExprMap[I] = Res; 00359 Res->setOperand(0, ConvertExpressionToType(I->getOperand(0),NewPTy,VMC,TD)); 00360 break; 00361 } 00362 default: 00363 assert(0 && "Expression convertible, but don't know how to convert?"); 00364 return 0; 00365 } 00366 00367 assert(Res->getType() == Ty && "Didn't convert expr to correct type!"); 00368 00369 BB->getInstList().insert(I, Res); 00370 00371 // Add the instruction to the expression map 00372 VMC.ExprMap[I] = Res; 00373 00374 00375 //// WTF is this code! FIXME: remove this. 00376 unsigned NumUses = I->getNumUses(); 00377 for (unsigned It = 0; It < NumUses; ) { 00378 unsigned OldSize = NumUses; 00379 Value::use_iterator UI = I->use_begin(); 00380 std::advance(UI, It); 00381 ConvertOperandToType(*UI, I, Res, VMC, TD); 00382 NumUses = I->getNumUses(); 00383 if (NumUses == OldSize) ++It; 00384 } 00385 00386 DEBUG(std::cerr << "ExpIn: " << (void*)I << " " << *I 00387 << "ExpOut: " << (void*)Res << " " << *Res); 00388 00389 return Res; 00390 } 00391 00392 00393 00394 // ValueConvertibleToType - Return true if it is possible 00395 bool llvm::ValueConvertibleToType(Value *V, const Type *Ty, 00396 ValueTypeCache &ConvertedTypes, 00397 const TargetData &TD) { 00398 ValueTypeCache::iterator I = ConvertedTypes.find(V); 00399 if (I != ConvertedTypes.end()) return I->second == Ty; 00400 ConvertedTypes[V] = Ty; 00401 00402 // It is safe to convert the specified value to the specified type IFF all of 00403 // the uses of the value can be converted to accept the new typed value. 00404 // 00405 if (V->getType() != Ty) { 00406 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) 00407 if (!OperandConvertibleToType(*I, V, Ty, ConvertedTypes, TD)) 00408 return false; 00409 } 00410 00411 return true; 00412 } 00413 00414 00415 00416 00417 00418 // OperandConvertibleToType - Return true if it is possible to convert operand 00419 // V of User (instruction) U to the specified type. This is true iff it is 00420 // possible to change the specified instruction to accept this. CTMap is a map 00421 // of converted types, so that circular definitions will see the future type of 00422 // the expression, not the static current type. 00423 // 00424 static bool OperandConvertibleToType(User *U, Value *V, const Type *Ty, 00425 ValueTypeCache &CTMap, 00426 const TargetData &TD) { 00427 // if (V->getType() == Ty) return true; // Operand already the right type? 00428 00429 // Expression type must be holdable in a register. 00430 if (!Ty->isFirstClassType()) 00431 return false; 00432 00433 Instruction *I = dyn_cast<Instruction>(U); 00434 if (I == 0) return false; // We can't convert! 00435 00436 switch (I->getOpcode()) { 00437 case Instruction::Cast: 00438 assert(I->getOperand(0) == V); 00439 // We can convert the expr if the cast destination type is losslessly 00440 // convertible to the requested type. 00441 // Also, do not change a cast that is a noop cast. For all intents and 00442 // purposes it should be eliminated. 00443 if (!Ty->isLosslesslyConvertibleTo(I->getOperand(0)->getType()) || 00444 I->getType() == I->getOperand(0)->getType()) 00445 return false; 00446 00447 // Do not allow a 'cast ushort %V to uint' to have it's first operand be 00448 // converted to a 'short' type. Doing so changes the way sign promotion 00449 // happens, and breaks things. Only allow the cast to take place if the 00450 // signedness doesn't change... or if the current cast is not a lossy 00451 // conversion. 00452 // 00453 if (!I->getType()->isLosslesslyConvertibleTo(I->getOperand(0)->getType()) && 00454 I->getOperand(0)->getType()->isSigned() != Ty->isSigned()) 00455 return false; 00456 00457 // We also do not allow conversion of a cast that casts from a ptr to array 00458 // of X to a *X. For example: cast [4 x %List *] * %val to %List * * 00459 // 00460 if (const PointerType *SPT = 00461 dyn_cast<PointerType>(I->getOperand(0)->getType())) 00462 if (const PointerType *DPT = dyn_cast<PointerType>(I->getType())) 00463 if (const ArrayType *AT = dyn_cast<ArrayType>(SPT->getElementType())) 00464 if (AT->getElementType() == DPT->getElementType()) 00465 return false; 00466 return true; 00467 00468 case Instruction::Add: 00469 case Instruction::Sub: { 00470 if (!Ty->isInteger() && !Ty->isFloatingPoint()) return false; 00471 00472 Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0); 00473 return ValueConvertibleToType(I, Ty, CTMap, TD) && 00474 ExpressionConvertibleToType(OtherOp, Ty, CTMap, TD); 00475 } 00476 case Instruction::SetEQ: 00477 case Instruction::SetNE: { 00478 Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0); 00479 return ExpressionConvertibleToType(OtherOp, Ty, CTMap, TD); 00480 } 00481 case Instruction::Shr: 00482 if (Ty->isSigned() != V->getType()->isSigned()) return false; 00483 // FALL THROUGH 00484 case Instruction::Shl: 00485 if (I->getOperand(1) == V) return false; // Cannot change shift amount type 00486 if (!Ty->isInteger()) return false; 00487 return ValueConvertibleToType(I, Ty, CTMap, TD); 00488 00489 case Instruction::Free: 00490 assert(I->getOperand(0) == V); 00491 return isa<PointerType>(Ty); // Free can free any pointer type! 00492 00493 case Instruction::Load: 00494 // Cannot convert the types of any subscripts... 00495 if (I->getOperand(0) != V) return false; 00496 00497 if (const PointerType *PT = dyn_cast<PointerType>(Ty)) { 00498 LoadInst *LI = cast<LoadInst>(I); 00499 00500 const Type *LoadedTy = PT->getElementType(); 00501 00502 // They could be loading the first element of a composite type... 00503 if (const CompositeType *CT = dyn_cast<CompositeType>(LoadedTy)) { 00504 unsigned Offset = 0; // No offset, get first leaf. 00505 std::vector<Value*> Indices; // Discarded... 00506 LoadedTy = getStructOffsetType(CT, Offset, Indices, TD, false); 00507 assert(Offset == 0 && "Offset changed from zero???"); 00508 } 00509 00510 if (!LoadedTy->isFirstClassType()) 00511 return false; 00512 00513 if (TD.getTypeSize(LoadedTy) != TD.getTypeSize(LI->getType())) 00514 return false; 00515 00516 return ValueConvertibleToType(LI, LoadedTy, CTMap, TD); 00517 } 00518 return false; 00519 00520 case Instruction::Store: { 00521 StoreInst *SI = cast<StoreInst>(I); 00522 00523 if (V == I->getOperand(0)) { 00524 ValueTypeCache::iterator CTMI = CTMap.find(I->getOperand(1)); 00525 if (CTMI != CTMap.end()) { // Operand #1 is in the table already? 00526 // If so, check to see if it's Ty*, or, more importantly, if it is a 00527 // pointer to a structure where the first element is a Ty... this code 00528 // is necessary because we might be trying to change the source and 00529 // destination type of the store (they might be related) and the dest 00530 // pointer type might be a pointer to structure. Below we allow pointer 00531 // to structures where the 0th element is compatible with the value, 00532 // now we have to support the symmetrical part of this. 00533 // 00534 const Type *ElTy = cast<PointerType>(CTMI->second)->getElementType(); 00535 00536 // Already a pointer to what we want? Trivially accept... 00537 if (ElTy == Ty) return true; 00538 00539 // Tricky case now, if the destination is a pointer to structure, 00540 // obviously the source is not allowed to be a structure (cannot copy 00541 // a whole structure at a time), so the level raiser must be trying to 00542 // store into the first field. Check for this and allow it now: 00543 // 00544 if (const StructType *SElTy = dyn_cast<StructType>(ElTy)) { 00545 unsigned Offset = 0; 00546 std::vector<Value*> Indices; 00547 ElTy = getStructOffsetType(ElTy, Offset, Indices, TD, false); 00548 assert(Offset == 0 && "Offset changed!"); 00549 if (ElTy == 0) // Element at offset zero in struct doesn't exist! 00550 return false; // Can only happen for {}* 00551 00552 if (ElTy == Ty) // Looks like the 0th element of structure is 00553 return true; // compatible! Accept now! 00554 00555 // Otherwise we know that we can't work, so just stop trying now. 00556 return false; 00557 } 00558 } 00559 00560 // Can convert the store if we can convert the pointer operand to match 00561 // the new value type... 00562 return ExpressionConvertibleToType(I->getOperand(1), PointerType::get(Ty), 00563 CTMap, TD); 00564 } else if (const PointerType *PT = dyn_cast<PointerType>(Ty)) { 00565 const Type *ElTy = PT->getElementType(); 00566 assert(V == I->getOperand(1)); 00567 00568 if (isa<StructType>(ElTy)) { 00569 // We can change the destination pointer if we can store our first 00570 // argument into the first element of the structure... 00571 // 00572 unsigned Offset = 0; 00573 std::vector<Value*> Indices; 00574 ElTy = getStructOffsetType(ElTy, Offset, Indices, TD, false); 00575 assert(Offset == 0 && "Offset changed!"); 00576 if (ElTy == 0) // Element at offset zero in struct doesn't exist! 00577 return false; // Can only happen for {}* 00578 } 00579 00580 // Must move the same amount of data... 00581 if (!ElTy->isSized() || 00582 TD.getTypeSize(ElTy) != TD.getTypeSize(I->getOperand(0)->getType())) 00583 return false; 00584 00585 // Can convert store if the incoming value is convertible and if the 00586 // result will preserve semantics... 00587 const Type *Op0Ty = I->getOperand(0)->getType(); 00588 if (!(Op0Ty->isIntegral() ^ ElTy->isIntegral()) && 00589 !(Op0Ty->isFloatingPoint() ^ ElTy->isFloatingPoint())) 00590 return ExpressionConvertibleToType(I->getOperand(0), ElTy, CTMap, TD); 00591 } 00592 return false; 00593 } 00594 00595 case Instruction::PHI: { 00596 PHINode *PN = cast<PHINode>(I); 00597 // Be conservative if we find a giant PHI node. 00598 if (PN->getNumIncomingValues() > 32) return false; 00599 00600 for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) 00601 if (!ExpressionConvertibleToType(PN->getIncomingValue(i), Ty, CTMap, TD)) 00602 return false; 00603 return ValueConvertibleToType(PN, Ty, CTMap, TD); 00604 } 00605 00606 case Instruction::Call: { 00607 User::op_iterator OI = std::find(I->op_begin(), I->op_end(), V); 00608 assert (OI != I->op_end() && "Not using value!"); 00609 unsigned OpNum = OI - I->op_begin(); 00610 00611 // Are we trying to change the function pointer value to a new type? 00612 if (OpNum == 0) { 00613 const PointerType *PTy = dyn_cast<PointerType>(Ty); 00614 if (PTy == 0) return false; // Can't convert to a non-pointer type... 00615 const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType()); 00616 if (FTy == 0) return false; // Can't convert to a non ptr to function... 00617 00618 // Do not allow converting to a call where all of the operands are ...'s 00619 if (FTy->getNumParams() == 0 && FTy->isVarArg()) 00620 return false; // Do not permit this conversion! 00621 00622 // Perform sanity checks to make sure that new function type has the 00623 // correct number of arguments... 00624 // 00625 unsigned NumArgs = I->getNumOperands()-1; // Don't include function ptr 00626 00627 // Cannot convert to a type that requires more fixed arguments than 00628 // the call provides... 00629 // 00630 if (NumArgs < FTy->getNumParams()) return false; 00631 00632 // Unless this is a vararg function type, we cannot provide more arguments 00633 // than are desired... 00634 // 00635 if (!FTy->isVarArg() && NumArgs > FTy->getNumParams()) 00636 return false; 00637 00638 // Okay, at this point, we know that the call and the function type match 00639 // number of arguments. Now we see if we can convert the arguments 00640 // themselves. Note that we do not require operands to be convertible, 00641 // we can insert casts if they are convertible but not compatible. The 00642 // reason for this is that we prefer to have resolved functions but casted 00643 // arguments if possible. 00644 // 00645 for (unsigned i = 0, NA = FTy->getNumParams(); i < NA; ++i) 00646 if (!FTy->getParamType(i)->isLosslesslyConvertibleTo(I->getOperand(i+1)->getType())) 00647 return false; // Operands must have compatible types! 00648 00649 // Okay, at this point, we know that all of the arguments can be 00650 // converted. We succeed if we can change the return type if 00651 // necessary... 00652 // 00653 return ValueConvertibleToType(I, FTy->getReturnType(), CTMap, TD); 00654 } 00655 00656 const PointerType *MPtr = cast<PointerType>(I->getOperand(0)->getType()); 00657 const FunctionType *FTy = cast<FunctionType>(MPtr->getElementType()); 00658 if (!FTy->isVarArg()) return false; 00659 00660 if ((OpNum-1) < FTy->getNumParams()) 00661 return false; // It's not in the varargs section... 00662 00663 // If we get this far, we know the value is in the varargs section of the 00664 // function! We can convert if we don't reinterpret the value... 00665 // 00666 return Ty->isLosslesslyConvertibleTo(V->getType()); 00667 } 00668 } 00669 return false; 00670 } 00671 00672 00673 void llvm::ConvertValueToNewType(Value *V, Value *NewVal, ValueMapCache &VMC, 00674 const TargetData &TD) { 00675 ValueHandle VH(VMC, V); 00676 00677 // FIXME: This is horrible! 00678 unsigned NumUses = V->getNumUses(); 00679 for (unsigned It = 0; It < NumUses; ) { 00680 unsigned OldSize = NumUses; 00681 Value::use_iterator UI = V->use_begin(); 00682 std::advance(UI, It); 00683 ConvertOperandToType(*UI, V, NewVal, VMC, TD); 00684 NumUses = V->getNumUses(); 00685 if (NumUses == OldSize) ++It; 00686 } 00687 } 00688 00689 00690 00691 static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, 00692 ValueMapCache &VMC, const TargetData &TD) { 00693 if (isa<ValueHandle>(U)) return; // Valuehandles don't let go of operands... 00694 00695 if (VMC.OperandsMapped.count(U)) return; 00696 VMC.OperandsMapped.insert(U); 00697 00698 ValueMapCache::ExprMapTy::iterator VMCI = VMC.ExprMap.find(U); 00699 if (VMCI != VMC.ExprMap.end()) 00700 return; 00701 00702 00703 Instruction *I = cast<Instruction>(U); // Only Instructions convertible 00704 00705 BasicBlock *BB = I->getParent(); 00706 assert(BB != 0 && "Instruction not embedded in basic block!"); 00707 std::string Name = I->getName(); 00708 I->setName(""); 00709 Instruction *Res; // Result of conversion 00710 00711 //std::cerr << endl << endl << "Type:\t" << Ty << "\nInst: " << I 00712 // << "BB Before: " << BB << endl; 00713 00714 // Prevent I from being removed... 00715 ValueHandle IHandle(VMC, I); 00716 00717 const Type *NewTy = NewVal->getType(); 00718 Constant *Dummy = (NewTy != Type::VoidTy) ? 00719 Constant::getNullValue(NewTy) : 0; 00720 00721 switch (I->getOpcode()) { 00722 case Instruction::Cast: 00723 if (VMC.NewCasts.count(ValueHandle(VMC, I))) { 00724 // This cast has already had it's value converted, causing a new cast to 00725 // be created. We don't want to create YET ANOTHER cast instruction 00726 // representing the original one, so just modify the operand of this cast 00727 // instruction, which we know is newly created. 00728 I->setOperand(0, NewVal); 00729 I->setName(Name); // give I its name back 00730 return; 00731 00732 } else { 00733 Res = new CastInst(NewVal, I->getType(), Name); 00734 } 00735 break; 00736 00737 case Instruction::Add: 00738 case Instruction::Sub: 00739 case Instruction::SetEQ: 00740 case Instruction::SetNE: { 00741 Res = BinaryOperator::create(cast<BinaryOperator>(I)->getOpcode(), 00742 Dummy, Dummy, Name); 00743 VMC.ExprMap[I] = Res; // Add node to expression eagerly 00744 00745 unsigned OtherIdx = (OldVal == I->getOperand(0)) ? 1 : 0; 00746 Value *OtherOp = I->getOperand(OtherIdx); 00747 Res->setOperand(!OtherIdx, NewVal); 00748 Value *NewOther = ConvertExpressionToType(OtherOp, NewTy, VMC, TD); 00749 Res->setOperand(OtherIdx, NewOther); 00750 break; 00751 } 00752 case Instruction::Shl: 00753 case Instruction::Shr: 00754 assert(I->getOperand(0) == OldVal); 00755 Res = new ShiftInst(cast<ShiftInst>(I)->getOpcode(), NewVal, 00756 I->getOperand(1), Name); 00757 break; 00758 00759 case Instruction::Free: // Free can free any pointer type! 00760 assert(I->getOperand(0) == OldVal); 00761 Res = new FreeInst(NewVal); 00762 break; 00763 00764 00765 case Instruction::Load: { 00766 assert(I->getOperand(0) == OldVal && isa<PointerType>(NewVal->getType())); 00767 const Type *LoadedTy = 00768 cast<PointerType>(NewVal->getType())->getElementType(); 00769 00770 Value *Src = NewVal; 00771 00772 if (const CompositeType *CT = dyn_cast<CompositeType>(LoadedTy)) { 00773 std::vector<Value*> Indices; 00774 Indices.push_back(Constant::getNullValue(Type::UIntTy)); 00775 00776 unsigned Offset = 0; // No offset, get first leaf. 00777 LoadedTy = getStructOffsetType(CT, Offset, Indices, TD, false); 00778 assert(LoadedTy->isFirstClassType()); 00779 00780 if (Indices.size() != 1) { // Do not generate load X, 0 00781 // Insert the GEP instruction before this load. 00782 Src = new GetElementPtrInst(Src, Indices, Name+".idx", I); 00783 } 00784 } 00785 00786 Res = new LoadInst(Src, Name); 00787 assert(Res->getType()->isFirstClassType() && "Load of structure or array!"); 00788 break; 00789 } 00790 00791 case Instruction::Store: { 00792 if (I->getOperand(0) == OldVal) { // Replace the source value 00793 // Check to see if operand #1 has already been converted... 00794 ValueMapCache::ExprMapTy::iterator VMCI = 00795 VMC.ExprMap.find(I->getOperand(1)); 00796 if (VMCI != VMC.ExprMap.end()) { 00797 // Comments describing this stuff are in the OperandConvertibleToType 00798 // switch statement for Store... 00799 // 00800 const Type *ElTy = 00801 cast<PointerType>(VMCI->second->getType())->getElementType(); 00802 00803 Value *SrcPtr = VMCI->second; 00804 00805 if (ElTy != NewTy) { 00806 // We check that this is a struct in the initial scan... 00807 const StructType *SElTy = cast<StructType>(ElTy); 00808 00809 std::vector<Value*> Indices; 00810 Indices.push_back(Constant::getNullValue(Type::UIntTy)); 00811 00812 unsigned Offset = 0; 00813 const Type *Ty = getStructOffsetType(ElTy, Offset, Indices, TD,false); 00814 assert(Offset == 0 && "Offset changed!"); 00815 assert(NewTy == Ty && "Did not convert to correct type!"); 00816 00817 // Insert the GEP instruction before this store. 00818 SrcPtr = new GetElementPtrInst(SrcPtr, Indices, 00819 SrcPtr->getName()+".idx", I); 00820 } 00821 Res = new StoreInst(NewVal, SrcPtr); 00822 00823 VMC.ExprMap[I] = Res; 00824 } else { 00825 // Otherwise, we haven't converted Operand #1 over yet... 00826 const PointerType *NewPT = PointerType::get(NewTy); 00827 Res = new StoreInst(NewVal, Constant::getNullValue(NewPT)); 00828 VMC.ExprMap[I] = Res; 00829 Res->setOperand(1, ConvertExpressionToType(I->getOperand(1), 00830 NewPT, VMC, TD)); 00831 } 00832 } else { // Replace the source pointer 00833 const Type *ValTy = cast<PointerType>(NewTy)->getElementType(); 00834 00835 Value *SrcPtr = NewVal; 00836 00837 if (isa<StructType>(ValTy)) { 00838 std::vector<Value*> Indices; 00839 Indices.push_back(Constant::getNullValue(Type::UIntTy)); 00840 00841 unsigned Offset = 0; 00842 ValTy = getStructOffsetType(ValTy, Offset, Indices, TD, false); 00843 00844 assert(Offset == 0 && ValTy); 00845 00846 // Insert the GEP instruction before this store. 00847 SrcPtr = new GetElementPtrInst(SrcPtr, Indices, 00848 SrcPtr->getName()+".idx", I); 00849 } 00850 00851 Res = new StoreInst(Constant::getNullValue(ValTy), SrcPtr); 00852 VMC.ExprMap[I] = Res; 00853 Res->setOperand(0, ConvertExpressionToType(I->getOperand(0), 00854 ValTy, VMC, TD)); 00855 } 00856 break; 00857 } 00858 00859 case Instruction::PHI: { 00860 PHINode *OldPN = cast<PHINode>(I); 00861 PHINode *NewPN = new PHINode(NewTy, Name); 00862 VMC.ExprMap[I] = NewPN; 00863 00864 while (OldPN->getNumOperands()) { 00865 BasicBlock *BB = OldPN->getIncomingBlock(0); 00866 Value *OldVal = OldPN->getIncomingValue(0); 00867 ValueHandle OldValHandle(VMC, OldVal); 00868 OldPN->removeIncomingValue(BB, false); 00869 Value *V = ConvertExpressionToType(OldVal, NewTy, VMC, TD); 00870 NewPN->addIncoming(V, BB); 00871 } 00872 Res = NewPN; 00873 break; 00874 } 00875 00876 case Instruction::Call: { 00877 Value *Meth = I->getOperand(0); 00878 std::vector<Value*> Params(I->op_begin()+1, I->op_end()); 00879 00880 if (Meth == OldVal) { // Changing the function pointer? 00881 const PointerType *NewPTy = cast<PointerType>(NewVal->getType()); 00882 const FunctionType *NewTy = cast<FunctionType>(NewPTy->getElementType()); 00883 00884 if (NewTy->getReturnType() == Type::VoidTy) 00885 Name = ""; // Make sure not to name a void call! 00886 00887 // Get an iterator to the call instruction so that we can insert casts for 00888 // operands if need be. Note that we do not require operands to be 00889 // convertible, we can insert casts if they are convertible but not 00890 // compatible. The reason for this is that we prefer to have resolved 00891 // functions but casted arguments if possible. 00892 // 00893 BasicBlock::iterator It = I; 00894 00895 // Convert over all of the call operands to their new types... but only 00896 // convert over the part that is not in the vararg section of the call. 00897 // 00898 for (unsigned i = 0; i != NewTy->getNumParams(); ++i) 00899 if (Params[i]->getType() != NewTy->getParamType(i)) { 00900 // Create a cast to convert it to the right type, we know that this 00901 // is a lossless cast... 00902 // 00903 Params[i] = new CastInst(Params[i], NewTy->getParamType(i), 00904 "callarg.cast." + 00905 Params[i]->getName(), It); 00906 } 00907 Meth = NewVal; // Update call destination to new value 00908 00909 } else { // Changing an argument, must be in vararg area 00910 std::vector<Value*>::iterator OI = 00911 std::find(Params.begin(), Params.end(), OldVal); 00912 assert (OI != Params.end() && "Not using value!"); 00913 00914 *OI = NewVal; 00915 } 00916 00917 Res = new CallInst(Meth, Params, Name); 00918 if (cast<CallInst>(I)->isTailCall()) 00919 cast<CallInst>(Res)->setTailCall(); 00920 cast<CallInst>(Res)->setCallingConv(cast<CallInst>(I)->getCallingConv()); 00921 break; 00922 } 00923 default: 00924 assert(0 && "Expression convertible, but don't know how to convert?"); 00925 return; 00926 } 00927 00928 // If the instruction was newly created, insert it into the instruction 00929 // stream. 00930 // 00931 BasicBlock::iterator It = I; 00932 assert(It != BB->end() && "Instruction not in own basic block??"); 00933 BB->getInstList().insert(It, Res); // Keep It pointing to old instruction 00934 00935 DEBUG(std::cerr << "COT CREATED: " << (void*)Res << " " << *Res 00936 << "In: " << (void*)I << " " << *I << "Out: " << (void*)Res 00937 << " " << *Res); 00938 00939 // Add the instruction to the expression map 00940 VMC.ExprMap[I] = Res; 00941 00942 if (I->getType() != Res->getType()) 00943 ConvertValueToNewType(I, Res, VMC, TD); 00944 else { 00945 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); 00946 UI != E; ) 00947 if (isa<ValueHandle>(*UI)) { 00948 ++UI; 00949 } else { 00950 Use &U = UI.getUse(); 00951 ++UI; // Do not invalidate UI. 00952 U.set(Res); 00953 } 00954 } 00955 } 00956 00957 00958 ValueHandle::ValueHandle(ValueMapCache &VMC, Value *V) 00959 : Instruction(Type::VoidTy, UserOp1, &Op, 1, ""), Op(V, this), Cache(VMC) { 00960 //DEBUG(std::cerr << "VH AQUIRING: " << (void*)V << " " << V); 00961 } 00962 00963 ValueHandle::ValueHandle(const ValueHandle &VH) 00964 : Instruction(Type::VoidTy, UserOp1, &Op, 1, ""), 00965 Op(VH.Op, this), Cache(VH.Cache) { 00966 //DEBUG(std::cerr << "VH AQUIRING: " << (void*)V << " " << V); 00967 } 00968 00969 static void RecursiveDelete(ValueMapCache &Cache, Instruction *I) { 00970 if (!I || !I->use_empty()) return; 00971 00972 assert(I->getParent() && "Inst not in basic block!"); 00973 00974 //DEBUG(std::cerr << "VH DELETING: " << (void*)I << " " << I); 00975 00976 for (User::op_iterator OI = I->op_begin(), OE = I->op_end(); 00977 OI != OE; ++OI) 00978 if (Instruction *U = dyn_cast<Instruction>(OI)) { 00979 *OI = 0; 00980 RecursiveDelete(Cache, U); 00981 } 00982 00983 I->getParent()->getInstList().remove(I); 00984 00985 Cache.OperandsMapped.erase(I); 00986 Cache.ExprMap.erase(I); 00987 delete I; 00988 } 00989 00990 ValueHandle::~ValueHandle() { 00991 if (Op->hasOneUse()) { 00992 Value *V = Op; 00993 Op.set(0); // Drop use! 00994 00995 // Now we just need to remove the old instruction so we don't get infinite 00996 // loops. Note that we cannot use DCE because DCE won't remove a store 00997 // instruction, for example. 00998 // 00999 RecursiveDelete(Cache, dyn_cast<Instruction>(V)); 01000 } else { 01001 //DEBUG(std::cerr << "VH RELEASING: " << (void*)Operands[0].get() << " " 01002 // << Operands[0]->getNumUses() << " " << Operands[0]); 01003 } 01004 } 01005