// See if we are doing a comparison with a constant.
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
- Value *A, *B;
+ Value *A = 0, *B = 0;
// (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B)
if (I.isEquality() && CI->isNullValue() &&
MaskedValueIsZero(Op0,
APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())))
return BinaryOperator::CreateLShr(Op0, I.getOperand(1));
-
+
+ // Arithmetic shifting an all-sign-bit value is a no-op.
+ unsigned NumSignBits = ComputeNumSignBits(Op0);
+ if (NumSignBits == Op0->getType()->getPrimitiveSizeInBits())
+ return ReplaceInstUsesWith(I, Op0);
+
return 0;
}
}
// If all of the base pointers of the PHI'd GEPs are from allocas, don't
- // bother doing this transformation. At best, this will just safe a bit of
+ // bother doing this transformation. At best, this will just save a bit of
// offset calculation, but all the predecessors will have to materialize the
// stack address into a register anyway. We'd actually rather *clone* the
// load up into the predecessors so that we have a load of a gep of an alloca,
}
-/// isSafeAndProfitableToSinkLoad - Return true if we know that it is safe sink
-/// the load out of the block that defines it. This means that it must be
+/// isSafeAndProfitableToSinkLoad - Return true if we know that it is safe to
+/// sink the load out of the block that defines it. This means that it must be
/// obvious the value of the load is not changed from the point of the load to
/// the end of the block it is in.
///
// transform: GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ...
// into : GEP [10 x i8]* X, i32 0, ...
//
+ // Likewise, transform: GEP (bitcast i8* X to [0 x i8]*), i32 0, ...
+ // into : GEP i8* X, ...
+ //
// This occurs when the program declares an array extern like "int X[];"
- //
const PointerType *CPTy = cast<PointerType>(PtrOp->getType());
const PointerType *XTy = cast<PointerType>(X->getType());
- if (const ArrayType *XATy =
- dyn_cast<ArrayType>(XTy->getElementType()))
- if (const ArrayType *CATy =
- dyn_cast<ArrayType>(CPTy->getElementType()))
+ if (const ArrayType *CATy =
+ dyn_cast<ArrayType>(CPTy->getElementType())) {
+ // GEP (bitcast i8* X to [0 x i8]*), i32 0, ... ?
+ if (CATy->getElementType() == XTy->getElementType()) {
+ // -> GEP i8* X, ...
+ SmallVector<Value*, 8> Indices(GEP.idx_begin()+1, GEP.idx_end());
+ return GetElementPtrInst::Create(X, Indices.begin(), Indices.end(),
+ GEP.getName());
+ } else if (const ArrayType *XATy =
+ dyn_cast<ArrayType>(XTy->getElementType())) {
+ // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ?
if (CATy->getElementType() == XATy->getElementType()) {
+ // -> GEP [10 x i8]* X, i32 0, ...
// At this point, we know that the cast source type is a pointer
// to an array of the same type as the destination pointer
// array. Because the array type is never stepped over (there
GEP.setOperand(0, X);
return &GEP;
}
+ }
+ }
} else if (GEP.getNumOperands() == 2) {
// Transform things like:
// %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V
// 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 &&
+ if (ArrayEltSize && Scale && Scale->getSExtValue() >= 0LL &&
Scale->getZExtValue() % ArrayEltSize == 0) {
Scale = ConstantInt::get(Scale->getType(),
Scale->getZExtValue() / ArrayEltSize);
BasicBlock::iterator InsertPos = DestBlock->getFirstNonPHI();
+ CopyPrecedingStopPoint(I, InsertPos);
I->moveBefore(InsertPos);
++NumSunkInst;
return true;
DBI_Prev->eraseFromParent();
}
DBI_Prev = DBI_Next;
+ } else {
+ DBI_Prev = 0;
}
IC.AddToWorkList(Inst);