(CI->getType()->getPrimitiveSizeInBits() ==
TD->getIntPtrType()->getPrimitiveSizeInBits())
&& isa<PointerType>(CI->getOperand(0)->getType())) {
+ unsigned AS =
+ cast<PointerType>(CI->getOperand(0)->getType())->getAddressSpace();
Value *I2 = InsertCastBefore(Instruction::BitCast, CI->getOperand(0),
- PointerType::get(Type::Int8Ty), I);
+ PointerType::get(Type::Int8Ty, AS), I);
I2 = InsertNewInstBefore(new GetElementPtrInst(I2, Other, "ctg2"), I);
return new PtrToIntInst(I2, CI->getType());
}
}
+
+ // add (select X 0 (sub n A)) A ->
+ // select X A n
+ {
+ SelectInst *SI = dyn_cast<SelectInst>(LHS);
+ Value *Other = RHS;
+ if (!SI) {
+ SI = dyn_cast<SelectInst>(RHS);
+ Other = LHS;
+ }
+ if (SI) {
+ Value *TV = SI->getTrueValue();
+ Value *FV = SI->getFalseValue();
+ Value *A;
+
+ // Can we fold the add into the argument of the select?
+ // We check both true and false select arguments for a matching subtract.
+ ConstantInt *C1, *C2;
+ if (match(FV, m_ConstantInt(C1)) && C1->getValue() == 0 &&
+ match(TV, m_Sub(m_ConstantInt(C2), m_Value(A))) &&
+ A == Other) {
+ // We managed to fold the add into the true select value.
+ return new SelectInst(SI->getCondition(), C2, A);
+ } else if (match(TV, m_ConstantInt(C1)) && C1->getValue() == 0 &&
+ match(FV, m_Sub(m_ConstantInt(C2), m_Value(A))) &&
+ A == Other) {
+ // We managed to fold the add into the false select value.
+ return new SelectInst(SI->getCondition(), A, C2);
+ }
+ }
+ }
return Changed ? &I : 0;
}
if (I.getType()->isInteger()) {
APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
+ // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
return BinaryOperator::createUDiv(Op0, Op1, I.getName());
}
}
if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
// X & 0xFFF0 is known to be a multiple of 16.
uint32_t Zeros = RHS->getValue().countTrailingZeros();
- if (Zeros != V->getType()->getPrimitiveSizeInBits())
+ if (Zeros != V->getType()->getPrimitiveSizeInBits())// don't shift by "32"
return ConstantExpr::getShl(Result,
ConstantInt::get(Result->getType(), Zeros));
}
Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
+ // Handle the integer rem common cases
if (Instruction *common = commonIRemTransforms(I))
return common;
return &I;
}
- // If the top bits of both operands are zero (i.e. we can prove they are
+ // If the sign bits of both operands are zero (i.e. we can prove they are
// unsigned inputs), turn this into a urem.
- APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
- if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
- // X srem Y -> X urem Y, iff X and Y don't have sign bit set
- return BinaryOperator::createURem(Op0, Op1, I.getName());
+ if (I.getType()->isInteger()) {
+ APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
+ if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
+ // X srem Y -> X urem Y, iff X and Y don't have sign bit set
+ return BinaryOperator::createURem(Op0, Op1, I.getName());
+ }
}
return 0;
LHSCC != ICmpInst::ICMP_UGE && LHSCC != ICmpInst::ICMP_ULE &&
RHSCC != ICmpInst::ICMP_UGE && RHSCC != ICmpInst::ICMP_ULE &&
LHSCC != ICmpInst::ICMP_SGE && LHSCC != ICmpInst::ICMP_SLE &&
- RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE) {
+ RHSCC != ICmpInst::ICMP_SGE && RHSCC != ICmpInst::ICMP_SLE &&
+
+ // Don't try to fold ICMP_SLT + ICMP_ULT.
+ (ICmpInst::isEquality(LHSCC) || ICmpInst::isEquality(RHSCC) ||
+ ICmpInst::isSignedPredicate(LHSCC) ==
+ ICmpInst::isSignedPredicate(RHSCC))) {
// Ensure that the larger constant is on the RHS.
ICmpInst::Predicate GT = ICmpInst::isSignedPredicate(LHSCC) ?
ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
case ICmpInst::ICMP_SGT:
switch (RHSCC) {
default: assert(0 && "Unknown integer condition code!");
- case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X s> 13
- return ReplaceInstUsesWith(I, LHS);
+ case ICmpInst::ICMP_EQ: // (X s> 13 & X == 15) -> X == 15
case ICmpInst::ICMP_SGT: // (X s> 13 & X s> 15) -> X s> 15
return ReplaceInstUsesWith(I, RHS);
case ICmpInst::ICMP_UGT: // (X s> 13 & X u> 15) -> no change
case ICmpInst::ICMP_EQ: // (X u< 13 | X == 14) -> no change
break;
case ICmpInst::ICMP_UGT: // (X u< 13 | X u> 15) ->(X-13) u> 2
+ // If RHSCst is [us]MAXINT, it is always false. Not handling
+ // this can cause overflow.
+ if (RHSCst->isMaxValue(false))
+ return ReplaceInstUsesWith(I, LHS);
return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), false,
false, I);
case ICmpInst::ICMP_SGT: // (X u< 13 | X s> 15) -> no change
case ICmpInst::ICMP_EQ: // (X s< 13 | X == 14) -> no change
break;
case ICmpInst::ICMP_SGT: // (X s< 13 | X s> 15) ->(X-13) s> 2
+ // If RHSCst is [us]MAXINT, it is always false. Not handling
+ // this can cause overflow.
+ if (RHSCst->isMaxValue(true))
+ return ReplaceInstUsesWith(I, LHS);
return InsertRangeTest(LHSVal, LHSCst, AddOne(RHSCst), true,
false, I);
case ICmpInst::ICMP_UGT: // (X s< 13 | X u> 15) -> no change
for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
Value *Op = GEP->getOperand(i);
- uint64_t Size = TD.getTypeSize(GTI.getIndexedType()) & PtrSizeMask;
+ uint64_t Size = TD.getABITypeSize(GTI.getIndexedType()) & PtrSizeMask;
if (ConstantInt *OpC = dyn_cast<ConstantInt>(Op)) {
if (OpC->isZero()) continue;
return ReplaceInstUsesWith(I, UndefValue::get(I.getType()));
if (C->isNullValue())
EmitIt = false;
- else if (TD->getTypeSize(GTI.getIndexedType()) == 0) {
+ else if (TD->getABITypeSize(GTI.getIndexedType()) == 0) {
EmitIt = false; // This is indexing into a zero sized array?
} else if (isa<ConstantInt>(C))
return ReplaceInstUsesWith(I, // No comparison is needed here.
if (isa<UndefValue>(Op1)) // X icmp undef -> undef
return ReplaceInstUsesWith(I, UndefValue::get(Type::Int1Ty));
-
+
+ // (icmp cond (sub m A) 0) ->
+ // (icmp cond m A)
+ {
+ ConstantInt *C1, *C2;
+ Value *A;
+ // Check both arguments of the compare for a matching subtract.
+ if (match(Op0, m_ConstantInt(C1)) && C1->getValue() == 0 &&
+ match(Op1, m_Sub(m_ConstantInt(C2), m_Value(A)))) {
+ // We managed to fold the add into the RHS of the select condition.
+ return new ICmpInst(I.getPredicate(), A, C2);
+ } else if (match(Op1, m_ConstantInt(C1)) && C1->getValue() == 0 &&
+ match(Op0, m_Sub(m_ConstantInt(C2), m_Value(A)))) {
+ // We managed to fold the add into the LHS of the select condition.
+ return new ICmpInst(I.getPredicate(), C2, A);
+ }
+ }
+
// icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
// addresses never equal each other! We already know that Op0 != Op1.
if ((isa<GlobalValue>(Op0) || isa<AllocaInst>(Op0) ||
}
Instruction *InstCombiner::visitAShr(BinaryOperator &I) {
- return commonShiftTransforms(I);
+ if (Instruction *R = commonShiftTransforms(I))
+ return R;
+
+ Value *Op0 = I.getOperand(0);
+
+ // ashr int -1, X = -1 (for any arithmetic shift rights of ~0)
+ if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
+ if (CSI->isAllOnesValue())
+ return ReplaceInstUsesWith(I, CSI);
+
+ // See if we can turn a signed shr into an unsigned shr.
+ if (MaskedValueIsZero(Op0,
+ APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())))
+ return BinaryOperator::createLShr(Op0, I.getOperand(1));
+
+ return 0;
}
Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) {
return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
}
- // ashr int -1, X = -1 (for any arithmetic shift rights of ~0)
- if (I.getOpcode() == Instruction::AShr)
- if (ConstantInt *CSI = dyn_cast<ConstantInt>(Op0))
- if (CSI->isAllOnesValue())
- return ReplaceInstUsesWith(I, CSI);
-
// Try to fold constant and into select arguments.
if (isa<Constant>(Op0))
if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
if (Instruction *R = FoldOpIntoSelect(I, SI, this))
return R;
- // See if we can turn a signed shr into an unsigned shr.
- if (I.isArithmeticShift()) {
- if (MaskedValueIsZero(Op0,
- APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()))) {
- return BinaryOperator::createLShr(Op0, Op1, I.getName());
- }
- }
-
if (ConstantInt *CUI = dyn_cast<ConstantInt>(Op1))
if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
return Res;
// the constant which would cause it to be modified for this
// operation.
//
- if (isValid && !isLeftShift && I.getOpcode() == Instruction::AShr) {
+ if (isValid && I.getOpcode() == Instruction::AShr)
isValid = Op0C->getValue()[TypeBits-1] == highBitSet;
- }
if (isValid) {
Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
// same, we open the door to infinite loops of various kinds.
if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return 0;
- uint64_t AllocElTySize = TD->getTypeSize(AllocElTy);
- uint64_t CastElTySize = TD->getTypeSize(CastElTy);
+ uint64_t AllocElTySize = TD->getABITypeSize(AllocElTy);
+ uint64_t CastElTySize = TD->getABITypeSize(CastElTy);
if (CastElTySize == 0 || AllocElTySize == 0) return 0;
// See if we can satisfy the modulus by pulling a scale out of the array
// is something like [0 x {int, int}]
const Type *IntPtrTy = TD->getIntPtrType();
int64_t FirstIdx = 0;
- if (int64_t TySize = TD->getTypeSize(GEPIdxTy)) {
+ if (int64_t TySize = TD->getABITypeSize(GEPIdxTy)) {
FirstIdx = Offset/TySize;
Offset %= TySize;
}
} else if (isa<ArrayType>(GEPIdxTy) || isa<VectorType>(GEPIdxTy)) {
const SequentialType *STy = cast<SequentialType>(GEPIdxTy);
- if (uint64_t EltSize = TD->getTypeSize(STy->getElementType())) {
+ if (uint64_t EltSize = TD->getABITypeSize(STy->getElementType())){
NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
Offset %= EltSize;
} else {
return BinaryOperator::createOr(NotCond, TrueVal);
}
}
+
+ // select a, b, a -> a&b
+ // select a, a, b -> a|b
+ if (CondVal == TrueVal)
+ return BinaryOperator::createOr(CondVal, FalseVal);
+ else if (CondVal == FalseVal)
+ return BinaryOperator::createAnd(CondVal, TrueVal);
}
// Selecting between two integer constants?
unsigned PrefAlign = 0) {
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
unsigned Align = GV->getAlignment();
- if (Align == 0 && TD)
+ if (Align == 0 && TD && GV->getType()->getElementType()->isSized())
Align = TD->getPrefTypeAlignment(GV->getType()->getElementType());
// If there is a large requested alignment and we can, bump up the alignment
// If Size is 2 then use Int16Ty
// If Size is 1 then use Int8Ty
if (Size && Size <=8 && !(Size&(Size-1)))
- NewPtrTy = PointerType::get(IntegerType::get(Size<<3));
+ NewPtrTy = PointerType::getUnqual(IntegerType::get(Size<<3));
if (NewPtrTy) {
- Value *Src = InsertCastBefore(Instruction::BitCast, CI.getOperand(2), NewPtrTy, CI);
- Value *Dest = InsertCastBefore(Instruction::BitCast, CI.getOperand(1), NewPtrTy, CI);
+ Value *Src = InsertCastBefore(Instruction::BitCast, CI.getOperand(2),
+ NewPtrTy, CI);
+ Value *Dest = InsertCastBefore(Instruction::BitCast, CI.getOperand(1),
+ NewPtrTy, CI);
Value *L = new LoadInst(Src, "tmp", false, Align, &CI);
Value *NS = new StoreInst(L, Dest, false, Align, &CI);
CI.replaceAllUsesWith(NS);
// Turn PPC lvx -> load if the pointer is known aligned.
// Turn X86 loadups -> load if the pointer is known aligned.
if (GetOrEnforceKnownAlignment(II->getOperand(1), TD, 16) >= 16) {
- Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(1),
- PointerType::get(II->getType()), CI);
+ Value *Ptr =
+ InsertCastBefore(Instruction::BitCast, II->getOperand(1),
+ PointerType::getUnqual(II->getType()), CI);
return new LoadInst(Ptr);
}
break;
case Intrinsic::ppc_altivec_stvxl:
// Turn stvx -> store if the pointer is known aligned.
if (GetOrEnforceKnownAlignment(II->getOperand(2), TD, 16) >= 16) {
- const Type *OpPtrTy = PointerType::get(II->getOperand(1)->getType());
+ const Type *OpPtrTy =
+ PointerType::getUnqual(II->getOperand(1)->getType());
Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(2),
OpPtrTy, CI);
return new StoreInst(II->getOperand(1), Ptr);
case Intrinsic::x86_sse2_storel_dq:
// Turn X86 storeu -> store if the pointer is known aligned.
if (GetOrEnforceKnownAlignment(II->getOperand(1), TD, 16) >= 16) {
- const Type *OpPtrTy = PointerType::get(II->getOperand(2)->getType());
+ const Type *OpPtrTy =
+ PointerType::getUnqual(II->getOperand(2)->getType());
Value *Ptr = InsertCastBefore(Instruction::BitCast, II->getOperand(1),
OpPtrTy, CI);
return new StoreInst(II->getOperand(2), Ptr);
// If the call and callee calling conventions don't match, this call must
// be unreachable, as the call is undefined.
new StoreInst(ConstantInt::getTrue(),
- UndefValue::get(PointerType::get(Type::Int1Ty)), OldCall);
+ UndefValue::get(PointerType::getUnqual(Type::Int1Ty)),
+ OldCall);
if (!OldCall->use_empty())
OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType()));
if (isa<CallInst>(OldCall)) // Not worth removing an invoke here.
// undef so that we know that this code is not reachable, despite the fact
// that we can't modify the CFG here.
new StoreInst(ConstantInt::getTrue(),
- UndefValue::get(PointerType::get(Type::Int1Ty)),
+ UndefValue::get(PointerType::getUnqual(Type::Int1Ty)),
CS.getInstruction());
if (!CS.getInstruction()->use_empty())
}
}
+ if (isa<InlineAsm>(Callee) && !CS.paramHasAttr(0, ParamAttr::NoUnwind)) {
+ // Inline asm calls cannot throw - mark them 'nounwind'.
+ const ParamAttrsList *PAL = CS.getParamAttrs();
+ uint16_t RAttributes = PAL ? PAL->getParamAttrs(0) : 0;
+ RAttributes |= ParamAttr::NoUnwind;
+
+ ParamAttrsVector modVec;
+ modVec.push_back(ParamAttrsWithIndex::get(0, RAttributes));
+ PAL = ParamAttrsList::getModified(PAL, modVec);
+ CS.setParamAttrs(PAL);
+ Changed = true;
+ }
+
return Changed ? CS.getInstruction() : 0;
}
const FunctionType *FT = Callee->getFunctionType();
const Type *OldRetTy = Caller->getType();
- const FunctionType *ActualFT =
- cast<FunctionType>(cast<PointerType>(CE->getType())->getElementType());
-
- // If the parameter attributes don't match up, don't do the xform. We don't
- // want to lose an sret attribute or something.
- if (FT->getParamAttrs() != ActualFT->getParamAttrs())
+ const ParamAttrsList* CallerPAL = 0;
+ if (CallInst *CallerCI = dyn_cast<CallInst>(Caller))
+ CallerPAL = CallerCI->getParamAttrs();
+ else if (InvokeInst *CallerII = dyn_cast<InvokeInst>(Caller))
+ CallerPAL = CallerII->getParamAttrs();
+
+ // If the parameter attributes are not compatible, don't do the xform. We
+ // don't want to lose an sret attribute or something.
+ if (!ParamAttrsList::areCompatible(CallerPAL, Callee->getParamAttrs()))
return false;
-
+
// Check to see if we are changing the return type...
if (OldRetTy != FT->getReturnType()) {
if (Callee->isDeclaration() && !Caller->use_empty() &&
NC = new InvokeInst(Callee, II->getNormalDest(), II->getUnwindDest(),
Args.begin(), Args.end(), Caller->getName(), Caller);
cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv());
+ cast<InvokeInst>(NC)->setParamAttrs(CallerPAL);
} else {
NC = new CallInst(Callee, Args.begin(), Args.end(),
Caller->getName(), Caller);
- if (cast<CallInst>(Caller)->isTailCall())
+ CallInst *CI = cast<CallInst>(Caller);
+ if (CI->isTailCall())
cast<CallInst>(NC)->setTailCall();
- cast<CallInst>(NC)->setCallingConv(cast<CallInst>(Caller)->getCallingConv());
+ cast<CallInst>(NC)->setCallingConv(CI->getCallingConv());
+ cast<CallInst>(NC)->setParamAttrs(CallerPAL);
}
// Insert a cast of the return type as necessary.
const PointerType *NestFPTy = cast<PointerType>(NestF->getType());
const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType());
- if (const ParamAttrsList *NestAttrs = NestFTy->getParamAttrs()) {
+ if (const ParamAttrsList *NestAttrs = NestF->getParamAttrs()) {
unsigned NestIdx = 1;
const Type *NestTy = 0;
uint16_t NestAttr = 0;
// Handle this by synthesizing a new function type, equal to FTy
// with the chain parameter inserted. Likewise for attributes.
- const ParamAttrsList *Attrs = FTy->getParamAttrs();
+ const ParamAttrsList *Attrs = CS.getParamAttrs();
std::vector<const Type*> NewTypes;
ParamAttrsVector NewAttrs;
NewTypes.reserve(FTy->getNumParams()+1);
// Replace the trampoline call with a direct call. Let the generic
// code sort out any function type mismatches.
FunctionType *NewFTy =
- FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg(),
- ParamAttrsList::get(NewAttrs));
- Constant *NewCallee = NestF->getType() == PointerType::get(NewFTy) ?
- NestF : ConstantExpr::getBitCast(NestF, PointerType::get(NewFTy));
+ FunctionType::get(FTy->getReturnType(), NewTypes, FTy->isVarArg());
+ Constant *NewCallee = NestF->getType() == PointerType::getUnqual(NewFTy) ?
+ NestF : ConstantExpr::getBitCast(NestF, PointerType::getUnqual(NewFTy));
+ const ParamAttrsList *NewPAL = ParamAttrsList::get(NewAttrs);
Instruction *NewCaller;
if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) {
NewArgs.begin(), NewArgs.end(),
Caller->getName(), Caller);
cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv());
+ cast<InvokeInst>(NewCaller)->setParamAttrs(NewPAL);
} else {
NewCaller = new CallInst(NewCallee, NewArgs.begin(), NewArgs.end(),
Caller->getName(), Caller);
cast<CallInst>(NewCaller)->setTailCall();
cast<CallInst>(NewCaller)->
setCallingConv(cast<CallInst>(Caller)->getCallingConv());
+ cast<CallInst>(NewCaller)->setParamAttrs(NewPAL);
}
if (Caller->getType() != Type::VoidTy && !Caller->use_empty())
Caller->replaceAllUsesWith(NewCaller);
return false;
}
+/// PHIsEqualValue - Return true if this phi node is always equal to
+/// NonPhiInVal. This happens with mutually cyclic phi nodes like:
+/// z = some value; x = phi (y, z); y = phi (x, z)
+static bool PHIsEqualValue(PHINode *PN, Value *NonPhiInVal,
+ SmallPtrSet<PHINode*, 16> &ValueEqualPHIs) {
+ // See if we already saw this PHI node.
+ if (!ValueEqualPHIs.insert(PN))
+ return true;
+
+ // Don't scan crazily complex things.
+ if (ValueEqualPHIs.size() == 16)
+ return false;
+
+ // Scan the operands to see if they are either phi nodes or are equal to
+ // the value.
+ for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
+ Value *Op = PN->getIncomingValue(i);
+ if (PHINode *OpPN = dyn_cast<PHINode>(Op)) {
+ if (!PHIsEqualValue(OpPN, NonPhiInVal, ValueEqualPHIs))
+ return false;
+ } else if (Op != NonPhiInVal)
+ return false;
+ }
+
+ return true;
+}
+
+
// PHINode simplification
//
Instruction *InstCombiner::visitPHINode(PHINode &PN) {
}
}
+ // We sometimes end up with phi cycles that non-obviously end up being the
+ // same value, for example:
+ // z = some value; x = phi (y, z); y = phi (x, z)
+ // where the phi nodes don't necessarily need to be in the same block. Do a
+ // quick check to see if the PHI node only contains a single non-phi value, if
+ // so, scan to see if the phi cycle is actually equal to that value.
+ {
+ unsigned InValNo = 0, NumOperandVals = PN.getNumIncomingValues();
+ // Scan for the first non-phi operand.
+ while (InValNo != NumOperandVals &&
+ isa<PHINode>(PN.getIncomingValue(InValNo)))
+ ++InValNo;
+
+ if (InValNo != NumOperandVals) {
+ Value *NonPhiInVal = PN.getOperand(InValNo);
+
+ // Scan the rest of the operands to see if there are any conflicts, if so
+ // there is no need to recursively scan other phis.
+ for (++InValNo; InValNo != NumOperandVals; ++InValNo) {
+ Value *OpVal = PN.getIncomingValue(InValNo);
+ if (OpVal != NonPhiInVal && !isa<PHINode>(OpVal))
+ break;
+ }
+
+ // If we scanned over all operands, then we have one unique value plus
+ // phi values. Scan PHI nodes to see if they all merge in each other or
+ // the value.
+ if (InValNo == NumOperandVals) {
+ SmallPtrSet<PHINode*, 16> ValueEqualPHIs;
+ if (PHIsEqualValue(&PN, NonPhiInVal, ValueEqualPHIs))
+ return ReplaceInstUsesWith(PN, NonPhiInVal);
+ }
+ }
+ }
return 0;
}
// insert it. This explicit cast can make subsequent optimizations more
// obvious.
Value *Op = GEP.getOperand(i);
- if (TD->getTypeSize(Op->getType()) > TD->getPointerSize())
+ if (TD->getTypeSizeInBits(Op->getType()) > TD->getPointerSizeInBits())
if (Constant *C = dyn_cast<Constant>(Op)) {
GEP.setOperand(i, ConstantExpr::getTrunc(C, TD->getIntPtrType()));
MadeChange = true;
} else if (Constant *GO1C = dyn_cast<Constant>(GO1)) {
GO1 = ConstantExpr::getIntegerCast(GO1C, SO1->getType(), true);
} else {
- unsigned PS = TD->getPointerSize();
- if (TD->getTypeSize(SO1->getType()) == PS) {
+ unsigned PS = TD->getPointerSizeInBits();
+ if (TD->getTypeSizeInBits(SO1->getType()) == PS) {
// Convert GO1 to SO1's type.
GO1 = InsertCastToIntPtrTy(GO1, SO1->getType(), &GEP, this);
- } else if (TD->getTypeSize(GO1->getType()) == PS) {
+ } else if (TD->getTypeSizeInBits(GO1->getType()) == PS) {
// Convert SO1 to GO1's type.
SO1 = InsertCastToIntPtrTy(SO1, GO1->getType(), &GEP, this);
} else {
if (!isa<PointerType>(X->getType())) {
// Not interesting. Source pointer must be a cast from pointer.
} else if (HasZeroPointerIndex) {
- // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ...
- // into : GEP [10 x ubyte]* X, long 0, ...
+ // transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ...
+ // into : GEP [10 x i8]* X, i32 0, ...
//
// This occurs when the program declares an array extern like "int X[];"
//
}
} else if (GEP.getNumOperands() == 2) {
// Transform things like:
- // %t = getelementptr ubyte* cast ([2 x int]* %str to uint*), uint %V
- // into: %t1 = getelementptr [2 x int*]* %str, int 0, uint %V; cast
+ // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V
+ // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast
const Type *SrcElTy = cast<PointerType>(X->getType())->getElementType();
const Type *ResElTy=cast<PointerType>(PtrOp->getType())->getElementType();
if (isa<ArrayType>(SrcElTy) &&
- TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
- TD->getTypeSize(ResElTy)) {
+ TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType()) ==
+ TD->getABITypeSize(ResElTy)) {
Value *Idx[2];
Idx[0] = Constant::getNullValue(Type::Int32Ty);
Idx[1] = GEP.getOperand(1);
}
// Transform things like:
- // getelementptr sbyte* cast ([100 x double]* X to sbyte*), int %tmp
+ // getelementptr i8* bitcast ([100 x double]* X to i8*), i32 %tmp
// (where tmp = 8*tmp2) into:
- // getelementptr [100 x double]* %arr, int 0, int %tmp.2
+ // getelementptr [100 x double]* %arr, i32 0, i32 %tmp2; bitcast
- if (isa<ArrayType>(SrcElTy) &&
- (ResElTy == Type::Int8Ty || ResElTy == Type::Int8Ty)) {
+ if (isa<ArrayType>(SrcElTy) && ResElTy == Type::Int8Ty) {
uint64_t ArrayEltSize =
- TD->getTypeSize(cast<ArrayType>(SrcElTy)->getElementType());
+ TD->getABITypeSize(cast<ArrayType>(SrcElTy)->getElementType());
// Check to see if "tmp" is a scale by a multiple of ArrayEltSize. We
// allow either a mul, shift, or constant here.
NewIdx = Inst->getOperand(0);
}
}
-
+
// If the index will be to exactly the right offset with the scale taken
- // out, perform the transformation.
- if (Scale && Scale->getZExtValue() % ArrayEltSize == 0) {
- if (isa<ConstantInt>(Scale))
- Scale = ConstantInt::get(Scale->getType(),
- Scale->getZExtValue() / ArrayEltSize);
+ // out, perform the transformation. Note, we don't know whether Scale is
+ // signed or not. We'll use unsigned version of division/modulo
+ // operation after making sure Scale doesn't have the sign bit set.
+ if (Scale && Scale->getSExtValue() >= 0LL &&
+ Scale->getZExtValue() % ArrayEltSize == 0) {
+ Scale = ConstantInt::get(Scale->getType(),
+ Scale->getZExtValue() / ArrayEltSize);
if (Scale->getZExtValue() != 1) {
Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(),
- true /*SExt*/);
+ false /*ZExt*/);
Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale");
NewIdx = InsertNewInstBefore(Sc, GEP);
}
// Note that we only do this for alloca's, because malloc should allocate and
// return a unique pointer, even for a zero byte allocation.
if (isa<AllocaInst>(AI) && AI.getAllocatedType()->isSized() &&
- TD->getTypeSize(AI.getAllocatedType()) == 0)
+ TD->getABITypeSize(AI.getAllocatedType()) == 0)
return ReplaceInstUsesWith(AI, Constant::getNullValue(AI.getType()));
return 0;
if (isa<UndefValue>(Op)) {
// Insert a new store to null because we cannot modify the CFG here.
new StoreInst(ConstantInt::getTrue(),
- UndefValue::get(PointerType::get(Type::Int1Ty)), &FI);
+ UndefValue::get(PointerType::getUnqual(Type::Int1Ty)), &FI);
return EraseInstFromFunction(FI);
}
return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1);
}
} else if (isa<LoadInst>(I)) {
+ unsigned AS =
+ cast<PointerType>(I->getOperand(0)->getType())->getAddressSpace();
Value *Ptr = InsertCastBefore(Instruction::BitCast, I->getOperand(0),
- PointerType::get(EI.getType()), EI);
+ PointerType::get(EI.getType(), AS), EI);
GetElementPtrInst *GEP =
new GetElementPtrInst(Ptr, EI.getOperand(1), I->getName() + ".gep");
InsertNewInstBefore(GEP, EI);