Change the interface to SCEVExpander::InsertCastOfTo to take a cast opcode

so the decision of which opcode to use is pushed upward to the caller.
Adjust the callers to pass the expected opcode.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@32535 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Reid Spencer 2006-12-13 08:06:42 +00:00
parent dccd9fe161
commit 3ba68b9eef
3 changed files with 40 additions and 35 deletions

View File

@ -88,7 +88,8 @@ namespace llvm {
/// InsertCastOfTo - Insert a cast of V to the specified type, doing what /// InsertCastOfTo - Insert a cast of V to the specified type, doing what
/// we can to share the casts. /// we can to share the casts.
static Value *InsertCastOfTo(Value *V, const Type *Ty); static Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V,
const Type *Ty);
protected: protected:
Value *expand(SCEV *S) { Value *expand(SCEV *S) {
@ -104,8 +105,20 @@ namespace llvm {
Value *expandInTy(SCEV *S, const Type *Ty) { Value *expandInTy(SCEV *S, const Type *Ty) {
Value *V = expand(S); Value *V = expand(S);
if (Ty && V->getType() != Ty) if (Ty && V->getType() != Ty) {
return InsertCastOfTo(V, Ty); if (isa<PointerType>(Ty) && V->getType()->isInteger())
return InsertCastOfTo(Instruction::IntToPtr, V, Ty);
else if (Ty->isInteger() && isa<PointerType>(V->getType()))
return InsertCastOfTo(Instruction::PtrToInt, V, Ty);
else if (Ty->getPrimitiveSizeInBits() ==
V->getType()->getPrimitiveSizeInBits())
return InsertCastOfTo(Instruction::BitCast, V, Ty);
else if (Ty->getPrimitiveSizeInBits() >
V->getType()->getPrimitiveSizeInBits())
return InsertCastOfTo(Instruction::ZExt, V, Ty);
else
return InsertCastOfTo(Instruction::Trunc, V, Ty);
}
return V; return V;
} }
@ -119,7 +132,7 @@ namespace llvm {
} }
Value *visitZeroExtendExpr(SCEVZeroExtendExpr *S) { Value *visitZeroExtendExpr(SCEVZeroExtendExpr *S) {
Value *V = expandInTy(S->getOperand(),S->getType()->getUnsignedVersion()); Value *V = expandInTy(S->getOperand(), S->getType());
return CastInst::createZExtOrBitCast(V, S->getType(), "tmp.", InsertPt); return CastInst::createZExtOrBitCast(V, S->getType(), "tmp.", InsertPt);
} }

View File

@ -19,25 +19,8 @@ using namespace llvm;
/// InsertCastOfTo - Insert a cast of V to the specified type, doing what /// InsertCastOfTo - Insert a cast of V to the specified type, doing what
/// we can to share the casts. /// we can to share the casts.
Value *SCEVExpander::InsertCastOfTo(Value *V, const Type *Ty) { Value *SCEVExpander::InsertCastOfTo(Instruction::CastOps opcode, Value *V,
// Compute the Cast opcode to use const Type *Ty) {
Instruction::CastOps opcode = Instruction::BitCast;
if (Ty->isIntegral()) {
if (V->getType()->getTypeID() == Type::PointerTyID)
opcode = Instruction::PtrToInt;
else {
unsigned SrcBits = V->getType()->getPrimitiveSizeInBits();
unsigned DstBits = Ty->getPrimitiveSizeInBits();
opcode = (SrcBits > DstBits ? Instruction::Trunc :
(SrcBits == DstBits ? Instruction::BitCast :
(V->getType()->isSigned() ? Instruction::SExt :
Instruction::ZExt)));
}
} else if (Ty->isFloatingPoint())
opcode = Instruction::UIToFP;
else if (Ty->getTypeID() == Type::PointerTyID && V->getType()->isIntegral())
opcode = Instruction::IntToPtr;
// FIXME: keep track of the cast instruction. // FIXME: keep track of the cast instruction.
if (Constant *C = dyn_cast<Constant>(V)) if (Constant *C = dyn_cast<Constant>(V))
return ConstantExpr::getCast(opcode, C, Ty); return ConstantExpr::getCast(opcode, C, Ty);

View File

@ -177,7 +177,7 @@ namespace {
/// getCastedVersionOf - Return the specified value casted to uintptr_t. /// getCastedVersionOf - Return the specified value casted to uintptr_t.
/// ///
Value *getCastedVersionOf(Value *V); Value *getCastedVersionOf(Instruction::CastOps opcode, Value *V);
private: private:
void runOnLoop(Loop *L); void runOnLoop(Loop *L);
bool AddUsersIfInteresting(Instruction *I, Loop *L, bool AddUsersIfInteresting(Instruction *I, Loop *L,
@ -203,19 +203,16 @@ FunctionPass *llvm::createLoopStrengthReducePass(const TargetLowering *TLI) {
/// getCastedVersionOf - Return the specified value casted to uintptr_t. This /// getCastedVersionOf - Return the specified value casted to uintptr_t. This
/// assumes that the Value* V is of integer or pointer type only. /// assumes that the Value* V is of integer or pointer type only.
/// ///
Value *LoopStrengthReduce::getCastedVersionOf(Value *V) { Value *LoopStrengthReduce::getCastedVersionOf(Instruction::CastOps opcode,
Value *V) {
if (V->getType() == UIntPtrTy) return V; if (V->getType() == UIntPtrTy) return V;
if (Constant *CB = dyn_cast<Constant>(V)) if (Constant *CB = dyn_cast<Constant>(V))
if (CB->getType()->isInteger()) return ConstantExpr::getCast(opcode, CB, UIntPtrTy);
return ConstantExpr::getIntegerCast(CB, UIntPtrTy,
CB->getType()->isSigned());
else
return ConstantExpr::getPtrToInt(CB, UIntPtrTy);
Value *&New = CastedPointers[V]; Value *&New = CastedPointers[V];
if (New) return New; if (New) return New;
New = SCEVExpander::InsertCastOfTo(V, UIntPtrTy); New = SCEVExpander::InsertCastOfTo(opcode, V, UIntPtrTy);
DeadInsts.insert(cast<Instruction>(New)); DeadInsts.insert(cast<Instruction>(New));
return New; return New;
} }
@ -258,7 +255,8 @@ SCEVHandle LoopStrengthReduce::GetExpressionSCEV(Instruction *Exp, Loop *L) {
// Build up the base expression. Insert an LLVM cast of the pointer to // Build up the base expression. Insert an LLVM cast of the pointer to
// uintptr_t first. // uintptr_t first.
SCEVHandle GEPVal = SCEVUnknown::get(getCastedVersionOf(GEP->getOperand(0))); SCEVHandle GEPVal = SCEVUnknown::get(
getCastedVersionOf(Instruction::PtrToInt, GEP->getOperand(0)));
gep_type_iterator GTI = gep_type_begin(GEP); gep_type_iterator GTI = gep_type_begin(GEP);
@ -273,7 +271,13 @@ SCEVHandle LoopStrengthReduce::GetExpressionSCEV(Instruction *Exp, Loop *L) {
GEPVal = SCEVAddExpr::get(GEPVal, GEPVal = SCEVAddExpr::get(GEPVal,
SCEVUnknown::getIntegerSCEV(Offset, UIntPtrTy)); SCEVUnknown::getIntegerSCEV(Offset, UIntPtrTy));
} else { } else {
Value *OpVal = getCastedVersionOf(GEP->getOperand(i)); unsigned GEPOpiBits =
GEP->getOperand(i)->getType()->getPrimitiveSizeInBits();
unsigned IntPtrBits = UIntPtrTy->getPrimitiveSizeInBits();
Instruction::CastOps opcode = (GEPOpiBits < IntPtrBits ?
Instruction::SExt : (GEPOpiBits > IntPtrBits ? Instruction::Trunc :
Instruction::BitCast));
Value *OpVal = getCastedVersionOf(opcode, GEP->getOperand(i));
SCEVHandle Idx = SE->getSCEV(OpVal); SCEVHandle Idx = SE->getSCEV(OpVal);
uint64_t TypeSize = TD->getTypeSize(GTI.getIndexedType()); uint64_t TypeSize = TD->getTypeSize(GTI.getIndexedType());
@ -1125,8 +1129,13 @@ void LoopStrengthReduce::StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
if (L->contains(User.Inst->getParent())) if (L->contains(User.Inst->getParent()))
User.Inst->moveBefore(LatchBlock->getTerminator()); User.Inst->moveBefore(LatchBlock->getTerminator());
} }
if (RewriteOp->getType() != ReplacedTy) if (RewriteOp->getType() != ReplacedTy) {
RewriteOp = SCEVExpander::InsertCastOfTo(RewriteOp, ReplacedTy); Instruction::CastOps opcode = Instruction::Trunc;
if (ReplacedTy->getPrimitiveSizeInBits() ==
RewriteOp->getType()->getPrimitiveSizeInBits())
opcode = Instruction::BitCast;
RewriteOp = SCEVExpander::InsertCastOfTo(opcode, RewriteOp, ReplacedTy);
}
SCEVHandle RewriteExpr = SCEVUnknown::get(RewriteOp); SCEVHandle RewriteExpr = SCEVUnknown::get(RewriteOp);