diff options
Diffstat (limited to 'lib/Transforms')
21 files changed, 908 insertions, 786 deletions
diff --git a/lib/Transforms/ExprTypeConvert.cpp b/lib/Transforms/ExprTypeConvert.cpp index 7c336d5b4b..39f21ead85 100644 --- a/lib/Transforms/ExprTypeConvert.cpp +++ b/lib/Transforms/ExprTypeConvert.cpp @@ -52,12 +52,10 @@ bool llvm::ExpressionConvertibleToType(Value *V, const Type *Ty, if (I == 0) return false; // Otherwise, we can't convert! switch (I->getOpcode()) { - case Instruction::Cast: - // We can convert the expr if the cast destination type is losslessly - // convertible to the requested type. - if (!Ty->isLosslesslyConvertibleTo(I->getType())) return false; - - // We also do not allow conversion of a cast that casts from a ptr to array + case Instruction::BitCast: + if (!cast<BitCastInst>(I)->isLosslessCast()) + return false; + // We do not allow conversion of a cast that casts from a ptr to array // of X to a *X. For example: cast [4 x %List *] * %val to %List * * // if (const PointerType *SPT = @@ -66,6 +64,7 @@ bool llvm::ExpressionConvertibleToType(Value *V, const Type *Ty, if (const ArrayType *AT = dyn_cast<ArrayType>(SPT->getElementType())) if (AT->getElementType() == DPT->getElementType()) return false; + // Otherwise it is a lossless cast and we can allow it break; case Instruction::Add: @@ -227,9 +226,9 @@ Value *llvm::ConvertExpressionToType(Value *V, const Type *Ty, Constant *Dummy = Constant::getNullValue(Ty); switch (I->getOpcode()) { - case Instruction::Cast: + case Instruction::BitCast: assert(VMC.NewCasts.count(ValueHandle(VMC, I)) == 0); - Res = new CastInst(I->getOperand(0), Ty, Name); + Res = CastInst::createInferredCast(I->getOperand(0), Ty, Name); VMC.NewCasts.insert(ValueHandle(VMC, Res)); break; @@ -307,7 +306,8 @@ Value *llvm::ConvertExpressionToType(Value *V, const Type *Ty, Indices.pop_back(); if (GetElementPtrInst::getIndexedType(BaseType, Indices, true) == PVTy) { if (Indices.size() == 0) - Res = new CastInst(GEP->getPointerOperand(), BaseType); // NOOP CAST + // We want to no-op cast this so use BitCast + Res = new BitCastInst(GEP->getPointerOperand(), BaseType); else Res = new GetElementPtrInst(GEP->getPointerOperand(), Indices, Name); break; @@ -411,10 +411,6 @@ bool llvm::ValueConvertibleToType(Value *V, const Type *Ty, return true; } - - - - // OperandConvertibleToType - Return true if it is possible to convert operand // V of User (instruction) U to the specified type. This is true iff it is // possible to change the specified instruction to accept this. CTMap is a map @@ -431,29 +427,18 @@ static bool OperandConvertibleToType(User *U, Value *V, const Type *Ty, return false; Instruction *I = dyn_cast<Instruction>(U); - if (I == 0) return false; // We can't convert! + if (I == 0) return false; // We can't convert non-instructions! switch (I->getOpcode()) { - case Instruction::Cast: + case Instruction::BitCast: assert(I->getOperand(0) == V); // We can convert the expr if the cast destination type is losslessly - // convertible to the requested type. - // Also, do not change a cast that is a noop cast. For all intents and - // purposes it should be eliminated. - if (!Ty->isLosslesslyConvertibleTo(I->getOperand(0)->getType()) || + // convertible to the requested type. Also, do not change a cast that + // is a noop cast. For all intents and purposes it should be eliminated. + if (!cast<BitCastInst>(I)->isLosslessCast() || I->getType() == I->getOperand(0)->getType()) return false; - // Do not allow a 'cast ushort %V to uint' to have it's first operand be - // converted to a 'short' type. Doing so changes the way sign promotion - // happens, and breaks things. Only allow the cast to take place if the - // signedness doesn't change... or if the current cast is not a lossy - // conversion. - // - if (!I->getType()->isLosslesslyConvertibleTo(I->getOperand(0)->getType()) && - I->getOperand(0)->getType()->isSigned() != Ty->isSigned()) - return false; - // We also do not allow conversion of a cast that casts from a ptr to array // of X to a *X. For example: cast [4 x %List *] * %val to %List * * // @@ -642,7 +627,8 @@ static bool OperandConvertibleToType(User *U, Value *V, const Type *Ty, // arguments if possible. // for (unsigned i = 0, NA = FTy->getNumParams(); i < NA; ++i) - if (!FTy->getParamType(i)->isLosslesslyConvertibleTo(I->getOperand(i+1)->getType())) + if (!FTy->getParamType(i)->canLosslesslyBitCastTo( + I->getOperand(i+1)->getType())) return false; // Operands must have compatible types! // Okay, at this point, we know that all of the arguments can be @@ -662,7 +648,7 @@ static bool OperandConvertibleToType(User *U, Value *V, const Type *Ty, // If we get this far, we know the value is in the varargs section of the // function! We can convert if we don't reinterpret the value... // - return Ty->isLosslesslyConvertibleTo(V->getType()); + return Ty->canLosslesslyBitCastTo(V->getType()); } } return false; @@ -718,19 +704,8 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, Constant::getNullValue(NewTy) : 0; switch (I->getOpcode()) { - case Instruction::Cast: - if (VMC.NewCasts.count(ValueHandle(VMC, I))) { - // This cast has already had it's value converted, causing a new cast to - // be created. We don't want to create YET ANOTHER cast instruction - // representing the original one, so just modify the operand of this cast - // instruction, which we know is newly created. - I->setOperand(0, NewVal); - I->setName(Name); // give I its name back - return; - - } else { - Res = new CastInst(NewVal, I->getType(), Name); - } + case Instruction::BitCast: + Res = CastInst::createInferredCast(NewVal, I->getType(), Name); break; case Instruction::Add: @@ -895,9 +870,9 @@ static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal, for (unsigned i = 0; i != NewTy->getNumParams(); ++i) if (Params[i]->getType() != NewTy->getParamType(i)) { // Create a cast to convert it to the right type, we know that this - // is a lossless cast... + // is a no-op cast... // - Params[i] = new CastInst(Params[i], NewTy->getParamType(i), + Params[i] = new BitCastInst(Params[i], NewTy->getParamType(i), "callarg.cast." + Params[i]->getName(), It); } diff --git a/lib/Transforms/IPO/GlobalOpt.cpp b/lib/Transforms/IPO/GlobalOpt.cpp index bdec082cd0..4ddc071681 100644 --- a/lib/Transforms/IPO/GlobalOpt.cpp +++ b/lib/Transforms/IPO/GlobalOpt.cpp @@ -329,7 +329,7 @@ static bool CleanupConstantGlobalUsers(Value *V, Constant *Init) { if (Init) SubInit = ConstantFoldLoadThroughGEPConstantExpr(Init, CE); Changed |= CleanupConstantGlobalUsers(CE, SubInit); - } else if (CE->getOpcode() == Instruction::Cast && + } else if (CE->getOpcode() == Instruction::BitCast && isa<PointerType>(CE->getType())) { // Pointer cast, delete any stores and memsets to the global. Changed |= CleanupConstantGlobalUsers(CE, 0); @@ -1174,7 +1174,7 @@ static void ShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) { LoadInst *NLI = new LoadInst(NewGV, Name+".b", LI); Value *NSI; if (IsOneZero) - NSI = new CastInst(NLI, LI->getType(), Name, LI); + NSI = CastInst::createInferredCast(NLI, LI->getType(), Name, LI); else NSI = new SelectInst(NLI, OtherVal, InitVal, Name, LI); LI->replaceAllUsesWith(NSI); diff --git a/lib/Transforms/IPO/IndMemRemoval.cpp b/lib/Transforms/IPO/IndMemRemoval.cpp index c993f70cec..0779a5414f 100644 --- a/lib/Transforms/IPO/IndMemRemoval.cpp +++ b/lib/Transforms/IPO/IndMemRemoval.cpp @@ -74,7 +74,8 @@ bool IndMemRemPass::runOnModule(Module &M) { GlobalValue::LinkOnceLinkage, "malloc_llvm_bounce", &M); BasicBlock* bb = new BasicBlock("entry",FN); - Instruction* c = new CastInst(FN->arg_begin(), Type::UIntTy, "c", bb); + Instruction* c = + CastInst::createInferredCast(FN->arg_begin(), Type::UIntTy, "c", bb); Instruction* a = new MallocInst(Type::SByteTy, c, "m", bb); new ReturnInst(a, bb); ++NumBounce; diff --git a/lib/Transforms/IPO/InlineSimple.cpp b/lib/Transforms/IPO/InlineSimple.cpp index c4b033c8c1..52d75735ef 100644 --- a/lib/Transforms/IPO/InlineSimple.cpp +++ b/lib/Transforms/IPO/InlineSimple.cpp @@ -141,14 +141,11 @@ void FunctionInfo::analyzeFunction(Function *F) { II != E; ++II) { if (isa<DbgInfoIntrinsic>(II)) continue; // Debug intrinsics don't count. - // Noop casts don't count. + // Noop casts, including ptr <-> int, don't count. if (const CastInst *CI = dyn_cast<CastInst>(II)) { - const Type *OpTy = CI->getOperand(0)->getType(); - if (CI->getType()->isLosslesslyConvertibleTo(OpTy)) + if (CI->isLosslessCast() || isa<IntToPtrInst>(CI) || + isa<PtrToIntInst>(CI)) continue; - if ((isa<PointerType>(CI->getType()) && OpTy->isInteger()) || - (isa<PointerType>(OpTy) && CI->getType()->isInteger())) - continue; // ptr <-> int is *probably* noop cast. } else if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(II)) { // If a GEP has all constant indices, it will probably be folded with diff --git a/lib/Transforms/IPO/LowerSetJmp.cpp b/lib/Transforms/IPO/LowerSetJmp.cpp index e6ff5c977a..3aac392091 100644 --- a/lib/Transforms/IPO/LowerSetJmp.cpp +++ b/lib/Transforms/IPO/LowerSetJmp.cpp @@ -259,7 +259,8 @@ void LowerSetJmp::TransformLongJmpCall(CallInst* Inst) // same parameters as "longjmp", except that the buffer is cast to a // char*. It returns "void", so it doesn't need to replace any of // Inst's uses and doesn't get a name. - CastInst* CI = new CastInst(Inst->getOperand(1), SBPTy, "LJBuf", Inst); + CastInst* CI = + new BitCastInst(Inst->getOperand(1), SBPTy, "LJBuf", Inst); new CallInst(ThrowLongJmp, make_vector<Value*>(CI, Inst->getOperand(2), 0), "", Inst); @@ -375,7 +376,8 @@ void LowerSetJmp::TransformSetJmpCall(CallInst* Inst) // Add this setjmp to the setjmp map. const Type* SBPTy = PointerType::get(Type::SByteTy); - CastInst* BufPtr = new CastInst(Inst->getOperand(1), SBPTy, "SBJmpBuf", Inst); + CastInst* BufPtr = + new BitCastInst(Inst->getOperand(1), SBPTy, "SBJmpBuf", Inst); new CallInst(AddSJToMap, make_vector<Value*>(GetSetJmpMap(Func), BufPtr, ConstantInt::get(Type::UIntTy, diff --git a/lib/Transforms/IPO/RaiseAllocations.cpp b/lib/Transforms/IPO/RaiseAllocations.cpp index 188b7f527f..584a2e98b0 100644 --- a/lib/Transforms/IPO/RaiseAllocations.cpp +++ b/lib/Transforms/IPO/RaiseAllocations.cpp @@ -140,7 +140,9 @@ bool RaiseAllocations::runOnModule(Module &M) { // If no prototype was provided for malloc, we may need to cast the // source size. if (Source->getType() != Type::UIntTy) - Source = new CastInst(Source, Type::UIntTy, "MallocAmtCast", I); + Source = + CastInst::createInferredCast(Source, Type::UIntTy, + "MallocAmtCast", I); std::string Name(I->getName()); I->setName(""); MallocInst *MI = new MallocInst(Type::SByteTy, Source, Name, I); @@ -160,7 +162,7 @@ bool RaiseAllocations::runOnModule(Module &M) { Users.insert(Users.end(), GV->use_begin(), GV->use_end()); EqPointers.push_back(GV); } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) { - if (CE->getOpcode() == Instruction::Cast) { + if (CE->isCast()) { Users.insert(Users.end(), CE->use_begin(), CE->use_end()); EqPointers.push_back(CE); } @@ -191,8 +193,8 @@ bool RaiseAllocations::runOnModule(Module &M) { // Value *Source = *CS.arg_begin(); if (!isa<PointerType>(Source->getType())) - Source = new CastInst(Source, PointerType::get(Type::SByteTy), - "FreePtrCast", I); + Source = CastInst::createInferredCast( + Source, PointerType::get(Type::SByteTy), "FreePtrCast", I); new FreeInst(Source, I); // If the old instruction was an invoke, add an unconditional branch @@ -211,7 +213,7 @@ bool RaiseAllocations::runOnModule(Module &M) { Users.insert(Users.end(), GV->use_begin(), GV->use_end()); EqPointers.push_back(GV); } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U)) { - if (CE->getOpcode() == Instruction::Cast) { + if (CE->isCast()) { Users.insert(Users.end(), CE->use_begin(), CE->use_end()); EqPointers.push_back(CE); } diff --git a/lib/Transforms/IPO/SimplifyLibCalls.cpp b/lib/Transforms/IPO/SimplifyLibCalls.cpp index af117094d6..fb22a2f139 100644 --- a/lib/Transforms/IPO/SimplifyLibCalls.cpp +++ b/lib/Transforms/IPO/SimplifyLibCalls.cpp @@ -650,7 +650,8 @@ public: LoadInst* load = new LoadInst(CastToCStr(s2,*ci), ci->getName()+".load",ci); CastInst* cast = - new CastInst(load,Type::IntTy,ci->getName()+".int",ci); + CastInst::create(Instruction::SExt, load, Type::IntTy, + ci->getName()+".int", ci); ci->replaceAllUsesWith(cast); ci->eraseFromParent(); return true; @@ -667,7 +668,8 @@ public: LoadInst* load = new LoadInst(CastToCStr(s1,*ci),ci->getName()+".val",ci); CastInst* cast = - new CastInst(load,Type::IntTy,ci->getName()+".int",ci); + CastInst::create(Instruction::SExt, load, Type::IntTy, + ci->getName()+".int", ci); ci->replaceAllUsesWith(cast); ci->eraseFromParent(); return true; @@ -741,7 +743,8 @@ public: // strncmp("",x) -> *x LoadInst* load = new LoadInst(s1,ci->getName()+".load",ci); CastInst* cast = - new CastInst(load,Type::IntTy,ci->getName()+".int",ci); + CastInst::create(Instruction::SExt, load, Type::IntTy, + ci->getName()+".int", ci); ci->replaceAllUsesWith(cast); ci->eraseFromParent(); return true; @@ -757,7 +760,8 @@ public: // strncmp(x,"") -> *x LoadInst* load = new LoadInst(s2,ci->getName()+".val",ci); CastInst* cast = - new CastInst(load,Type::IntTy,ci->getName()+".int",ci); + CastInst::create(Instruction::SExt, load, Type::IntTy, + ci->getName()+".int", ci); ci->replaceAllUsesWith(cast); ci->eraseFromParent(); return true; @@ -997,13 +1001,15 @@ struct memcmpOptimization : public LibCallOptimization { case 1: { // memcmp(S1,S2,1) -> *(ubyte*)S1 - *(ubyte*)S2 const Type *UCharPtr = PointerType::get(Type::UByteTy); - CastInst *Op1Cast = new CastInst(LHS, UCharPtr, LHS->getName(), CI); - CastInst *Op2Cast = new CastInst(RHS, UCharPtr, RHS->getName(), CI); + CastInst *Op1Cast = CastInst::create( + Instruction::BitCast, LHS, UCharPtr, LHS->getName(), CI); + CastInst *Op2Cast = CastInst::create( + Instruction::BitCast, RHS, UCharPtr, RHS->getName(), CI); Value *S1V = new LoadInst(Op1Cast, LHS->getName()+".val", CI); Value *S2V = new LoadInst(Op2Cast, RHS->getName()+".val", CI); Value *RV = BinaryOperator::createSub(S1V, S2V, CI->getName()+".diff",CI); if (RV->getType() != CI->getType()) - RV = new CastInst(RV, CI->getType(), RV->getName(), CI); + RV = CastInst::createInferredCast(RV, CI->getType(), RV->getName(), CI); CI->replaceAllUsesWith(RV); CI->eraseFromParent(); return true; @@ -1014,8 +1020,10 @@ struct memcmpOptimization : public LibCallOptimization { // memcmp(S1,S2,2) -> S1[0]-S2[0] | S1[1]-S2[1] iff only ==/!= 0 matters const Type *UCharPtr = PointerType::get(Type::UByteTy); - CastInst *Op1Cast = new CastInst(LHS, UCharPtr, LHS->getName(), CI); - CastInst *Op2Cast = new CastInst(RHS, UCharPtr, RHS->getName(), CI); + CastInst *Op1Cast = CastInst::create( + Instruction::BitCast, LHS, UCharPtr, LHS->getName(), CI); + CastInst *Op2Cast = CastInst::create( + Instruction::BitCast, RHS, UCharPtr, RHS->getName(), CI); Value *S1V1 = new LoadInst(Op1Cast, LHS->getName()+".val1", CI); Value *S2V1 = new LoadInst(Op2Cast, RHS->getName()+".val1", CI); Value *D1 = BinaryOperator::createSub(S1V1, S2V1, @@ -1029,7 +1037,8 @@ struct memcmpOptimization : public LibCallOptimization { CI->getName()+".d1", CI); Value *Or = BinaryOperator::createOr(D1, D2, CI->getName()+".res", CI); if (Or->getType() != CI->getType()) - Or = new CastInst(Or, CI->getType(), Or->getName(), CI); + Or = CastInst::createInferredCast(Or, CI->getType(), Or->getName(), + CI); CI->replaceAllUsesWith(Or); CI->eraseFromParent(); return true; @@ -1101,10 +1110,10 @@ struct LLVMMemCpyMoveOptzn : public LibCallOptimization { } // Cast source and dest to the right sized primitive and then load/store - CastInst* SrcCast = - new CastInst(src,PointerType::get(castType),src->getName()+".cast",ci); - CastInst* DestCast = - new CastInst(dest,PointerType::get(castType),dest->getName()+".cast",ci); + CastInst* SrcCast = CastInst::create(Instruction::BitCast, + src, PointerType::get(castType), src->getName()+".cast", ci); + CastInst* DestCast = CastInst::create(Instruction::BitCast, + dest, PointerType::get(castType),dest->getName()+".cast", ci); LoadInst* LI = new LoadInst(SrcCast,SrcCast->getName()+".val",ci); new StoreInst(LI, DestCast, ci); ci->eraseFromParent(); @@ -1213,8 +1222,8 @@ struct LLVMMemSetOptimization : public LibCallOptimization { } // Cast dest to the right sized primitive and then load/store - CastInst* DestCast = - new CastInst(dest,PointerType::get(castType),dest->getName()+".cast",ci); + CastInst* DestCast = CastInst::createInferredCast( + dest, PointerType::get(castType), dest->getName()+".cast", ci); new StoreInst(ConstantInt::get(castType,fill_value),DestCast, ci); ci->eraseFromParent(); return true; @@ -1356,8 +1365,8 @@ public: Function* putchar_func = SLC.get_putchar(); if (!putchar_func) return false; - CastInst* cast = new CastInst(ci->getOperand(2), Type::IntTy, - CI->getName()+".int", ci); + CastInst* cast = CastInst::createInferredCast( + ci->getOperand(2), Type::IntTy, CI->getName()+".int", ci); new CallInst(putchar_func, cast, "", ci); ci->replaceAllUsesWith(ConstantInt::get(Type::IntTy, 1)); break; @@ -1490,8 +1499,8 @@ public: Function* fputc_func = SLC.get_fputc(FILEptr_type); if (!fputc_func) return false; - CastInst* cast = new CastInst(ci->getOperand(3), Type::IntTy, - CI->getName()+".int", ci); + CastInst* cast = CastInst::createInferredCast( + ci->getOperand(3), Type::IntTy, CI->getName()+".int", ci); new CallInst(fputc_func,cast,ci->getOperand(1),"",ci); ci->replaceAllUsesWith(ConstantInt::get(Type::IntTy,1)); break; @@ -1597,7 +1606,8 @@ public: ConstantInt::get(Len->getType(), 1), Len->getName()+"1", ci); if (Len1->getType() != SLC.getIntPtrType()) - Len1 = new CastInst(Len1, SLC.getIntPtrType(), Len1->getName(), ci); + Len1 = CastInst::createInferredCast( + Len1, SLC.getIntPtrType(), Len1->getName(), ci); std::vector<Value*> args; args.push_back(CastToCStr(ci->getOperand(1), *ci)); args.push_back(CastToCStr(ci->getOperand(3), *ci)); @@ -1608,7 +1618,8 @@ public: // The strlen result is the unincremented number of bytes in the string. if (!ci->use_empty()) { if (Len->getType() != ci->getType()) - Len = new CastInst(Len, ci->getType(), Len->getName(), ci); + Len = CastInst::createInferredCast( + Len, ci->getType(), Len->getName(), ci); ci->replaceAllUsesWith(Len); } ci->eraseFromParent(); @@ -1616,7 +1627,8 @@ public: } case 'c': { // sprintf(dest,"%c",chr) -> store chr, dest - CastInst* cast = new CastInst(ci->getOperand(3),Type::SByteTy,"char",ci); + CastInst* cast = CastInst::createInferredCast( + ci->getOperand(3), Type::SByteTy, "char", ci); new StoreInst(cast, ci->getOperand(1), ci); GetElementPtrInst* gep = new GetElementPtrInst(ci->getOperand(1), ConstantInt::get(Type::UIntTy,1),ci->getOperand(1)->getName()+".end", @@ -1672,8 +1684,8 @@ public: return false; LoadInst* loadi = new LoadInst(ci->getOperand(1), ci->getOperand(1)->getName()+".byte",ci); - CastInst* casti = new CastInst(loadi,Type::IntTy, - loadi->getName()+".int",ci); + CastInst* casti = CastInst::createInferredCast( + loadi, Type::IntTy, loadi->getName()+".int", ci); new CallInst(fputc_func,casti,ci->getOperand(2),"",ci); break; } @@ -1726,18 +1738,16 @@ public: } // isdigit(c) -> (unsigned)c - '0' <= 9 - CastInst* cast = - new CastInst(ci->getOperand(1),Type::UIntTy, - ci->getOperand(1)->getName()+".uint",ci); + CastInst* cast = CastInst::createInferredCast(ci->getOperand(1), + Type::UIntTy, ci->getOperand(1)->getName()+".uint", ci); BinaryOperator* sub_inst = BinaryOperator::createSub(cast, ConstantInt::get(Type::UIntTy,0x30), ci->getOperand(1)->getName()+".sub",ci); SetCondInst* setcond_inst = new SetCondInst(Instruction::SetLE,sub_inst, ConstantInt::get(Type::UIntTy,9), ci->getOperand(1)->getName()+".cmp",ci); - CastInst* c2 = - new CastInst(setcond_inst,Type::IntTy, - ci->getOperand(1)->getName()+".isdigit",ci); + CastInst* c2 = CastInst::createInferredCast( + setcond_inst, Type::IntTy, ci->getOperand(1)->getName()+".isdigit", ci); ci->replaceAllUsesWith(c2); ci->eraseFromParent(); return true; @@ -1759,12 +1769,14 @@ public: // isascii(c) -> (unsigned)c < 128 Value *V = CI->getOperand(1); if (V->getType()->isSigned()) - V = new CastInst(V, V->getType()->getUnsignedVersion(), V->getName(), CI); + V = CastInst::createInferredCast(V, V->getType()->getUnsignedVersion(), + V->getName(), CI); Value *Cmp = BinaryOperator::createSetLT(V, ConstantInt::get(V->getType(), 128), V->getName()+".isascii", CI); if (Cmp->getType() != CI->getType()) - Cmp = new CastInst(Cmp, CI->getType(), Cmp->getName(), CI); + Cmp = CastInst::createInferredCast( + Cmp, CI->getType(), Cmp->getName(), CI); CI->replaceAllUsesWith(Cmp); CI->eraseFromParent(); return true; @@ -1858,9 +1870,10 @@ public: Function *F = SLC.getModule()->getOrInsertFunction(CTTZName, ArgType, ArgType, NULL); - Value *V = new CastInst(TheCall->getOperand(1), ArgType, "tmp", TheCall); + Value *V = CastInst::createInferredCast( + TheCall->getOperand(1), ArgType, "tmp", TheCall); Value *V2 = new CallInst(F, V, "tmp", TheCall); - V2 = new CastInst(V2, Type::IntTy, "tmp", TheCall); + V2 = CastInst::createInferredCast(V2, Type::IntTy, "tmp", TheCall); V2 = BinaryOperator::createAdd(V2, ConstantInt::get(Type::IntTy, 1), "tmp", TheCall); Value *Cond = @@ -1920,7 +1933,7 @@ struct UnaryDoubleFPOptimizer : public LibCallOptimization { if (Cast->getOperand(0)->getType() == Type::FloatTy) { Value *New = new CallInst((SLC.*FP)(), Cast->getOperand(0), CI->getName(), CI); - New = new CastInst(New, Type::DoubleTy, CI->getName(), CI); + New = new FPExtInst(New, Type::DoubleTy, CI->getName(), CI); CI->replaceAllUsesWith(New); CI->eraseFromParent(); if (Cast->use_empty()) @@ -2105,7 +2118,7 @@ bool getConstantStringLength(Value *V, uint64_t &len, ConstantArray **CA) { Value *CastToCStr(Value *V, Instruction &IP) { const Type *SBPTy = PointerType::get(Type::SByteTy); if (V->getType() != SBPTy) - return new CastInst(V, SBPTy, V->getName(), &IP); + return CastInst::createInferredCast(V, SBPTy, V->getName(), &IP); return V; } diff --git a/lib/Transforms/Instrumentation/ProfilingUtils.cpp b/lib/Transforms/Instrumentation/ProfilingUtils.cpp index 4c31793f42..887de5bfe2 100644 --- a/lib/Transforms/Instrumentation/ProfilingUtils.cpp +++ b/lib/Transforms/Instrumentation/ProfilingUtils.cpp @@ -62,7 +62,8 @@ void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName, case 2: AI = MainFn->arg_begin(); ++AI; if (AI->getType() != ArgVTy) { - InitCall->setOperand(2, new CastInst(AI, ArgVTy, "argv.cast", InitCall)); + InitCall->setOperand(2, + CastInst::createInferredCast(AI, ArgVTy, "argv.cast", InitCall)); } else { InitCall->setOperand(2, AI); } @@ -73,10 +74,10 @@ void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName, // init call instead. if (AI->getType() != Type::IntTy) { if (!AI->use_empty()) - AI->replaceAllUsesWith(new CastInst(InitCall, AI->getType(), "", - InsertPos)); - InitCall->setOperand(1, new CastInst(AI, Type::IntTy, "argc.cast", - InitCall)); + AI->replaceAllUsesWith( + CastInst::createInferredCast(InitCall, AI->getType(), "", InsertPos)); + InitCall->setOperand(1, + CastInst::createInferredCast(AI, Type::IntTy, "argc.cast", InitCall)); } else { AI->replaceAllUsesWith(InitCall); InitCall->setOperand(1, AI); diff --git a/lib/Transforms/Instrumentation/TraceValues.cpp b/lib/Transforms/Instrumentation/TraceValues.cpp index d335f16cad..7451b51c31 100644 --- a/lib/Transforms/Instrumentation/TraceValues.cpp +++ b/lib/Transforms/Instrumentation/TraceValues.cpp @@ -190,8 +190,7 @@ static inline bool TraceThisOpCode(unsigned opCode) { // return (opCode < Instruction::OtherOpsBegin && opCode != Instruction::Alloca && - opCode != Instruction::PHI && - opCode != Instruction::Cast); + opCode != Instruction::PHI && ! Instruction::isCast(opCode)); } @@ -251,7 +250,7 @@ static void InsertPrintInst(Value *V, BasicBlock *BB, Instruction *InsertBefore, if (V && isa<PointerType>(V->getType()) && !DisablePtrHashing) { const Type *SBP = PointerType::get(Type::SByteTy); if (V->getType() != SBP) // Cast pointer to be sbyte* - V = new CastInst(V, SBP, "Hash_cast", InsertBefore); + V = new BitCastInst(V, SBP, "Hash_cast", InsertBefore); std::vector<Value*> HashArgs(1, V); V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum", InsertBefore); @@ -282,7 +281,7 @@ InsertReleaseInst(Value *V, BasicBlock *BB, const Type *SBP = PointerType::get(Type::SByteTy); if (V->getType() != SBP) // Cast pointer to be sbyte* - V = new CastInst(V, SBP, "RPSN_cast", InsertBefore); + V = CastInst::createInferredCast(V, SBP, "RPSN_cast", InsertBefore); std::vector<Value*> releaseArgs(1, V); new CallInst(ReleasePtrFunc, releaseArgs, "", InsertBefore); @@ -294,7 +293,7 @@ InsertRecordInst(Value *V, BasicBlock *BB, Function* RecordPtrFunc) { const Type *SBP = PointerType::get(Type::SByteTy); if (V->getType() != SBP) // Cast pointer to be sbyte* - V = new CastInst(V, SBP, "RP_cast", InsertBefore); + V = CastInst::createInferredCast(V, SBP, "RP_cast", InsertBefore); std::vector<Value*> releaseArgs(1, V); new CallInst(RecordPtrFunc, releaseArgs, "", InsertBefore); diff --git a/lib/Transforms/LevelRaise.cpp b/lib/Transforms/LevelRaise.cpp index 7713609907..c8635e181f 100644 --- a/lib/Transforms/LevelRaise.cpp +++ b/lib/Transforms/LevelRaise.cpp @@ -87,15 +87,6 @@ FunctionPass *llvm::createRaisePointerReferencesPass() { return new RPR(); } - -// isReinterpretingCast - Return true if the cast instruction specified will -// cause the operand to be "reinterpreted". A value is reinterpreted if the -// cast instruction would cause the underlying bits to change. -// -static inline bool isReinterpretingCast(const CastInst *CI) { - return!CI->getOperand(0)->getType()->isLosslesslyConvertibleTo(CI->getType()); -} - bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { Instruction *I = BI; const TargetData &TD = getAnalysis<TargetData>(); @@ -129,7 +120,7 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { // Check to see if it's a cast of an instruction that does not depend on the // specific type of the operands to do it's job. - if (!isReinterpretingCast(CI)) { + if (CI->isLosslessCast()) { ValueTypeCache ConvertedTypes; // Check to see if we can convert the source of the cast to match the @@ -238,7 +229,7 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { Indices.push_back(Constant::getNullValue(Type::UIntTy)); // Did we find what we're looking for? - if (ElTy->isLosslesslyConvertibleTo(DestPointedTy)) break; + if (ElTy->canLosslesslyBitCastTo(DestPointedTy)) break; // Nope, go a level deeper. ++Depth; @@ -257,9 +248,23 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { Name, BI); // Make the old cast instruction reference the new GEP instead of - // the old src value. - // - CI->setOperand(0, GEP); + // the old src value. + if (CI->getOperand(0)->getType() == GEP->getType()) { + // If the source types are the same we can safely replace the + // first operand of the CastInst because the opcode won't + // change as a result. + CI->setOperand(0, GEP); + } else { + // The existing and new operand 0 types are different so we must + // replace CI with a new CastInst so that we are assured to + // get the correct cast opcode. + CastInst *NewCI = CastInst::createInferredCast( + GEP, CI->getType(), CI->getName(), CI); + CI->replaceAllUsesWith(NewCI); + CI->eraseFromParent(); + CI = NewCI; + BI = NewCI; // Don't let the iterator invalidate + } PRINT_PEEPHOLE2("cast-for-first:out", *GEP, *CI); ++NumGEPInstFormed; @@ -273,7 +278,7 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { Value *Pointer = SI->getPointerOperand(); // Peephole optimize the following instructions: - // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertible to T2 + // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly castable to T2 // store <T2> %V, <T2>* %t // // Into: @@ -289,13 +294,14 @@ bool RPR::PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) { if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType if (const PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType())) // convertible types? - if (Val->getType()->isLosslesslyConvertibleTo(CSPT->getElementType())) { + |