fix ConstantFoldCompareInstOperands to take the LHS/RHS as

individual operands instead of taking a temporary array

llvm-svn: 86619
This commit is contained in:
Chris Lattner 2009-11-09 23:06:58 +00:00
parent 3f94a132dd
commit cdfb80de16
5 changed files with 28 additions and 36 deletions

View File

@ -55,7 +55,7 @@ Constant *ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
/// returns a constant expression of the specified operands. /// returns a constant expression of the specified operands.
/// ///
Constant *ConstantFoldCompareInstOperands(unsigned Predicate, Constant *ConstantFoldCompareInstOperands(unsigned Predicate,
Constant *const *Ops, unsigned NumOps, Constant *LHS, Constant *RHS,
const TargetData *TD = 0); const TargetData *TD = 0);
/// ConstantFoldLoadFromConstPtr - Return the value that a load from C would /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would

View File

@ -655,8 +655,8 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I, const TargetData *TD) {
return 0; // All operands not constant! return 0; // All operands not constant!
if (const CmpInst *CI = dyn_cast<CmpInst>(I)) if (const CmpInst *CI = dyn_cast<CmpInst>(I))
return ConstantFoldCompareInstOperands(CI->getPredicate(), return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
Ops.data(), Ops.size(), TD); TD);
if (const LoadInst *LI = dyn_cast<LoadInst>(I)) if (const LoadInst *LI = dyn_cast<LoadInst>(I))
return ConstantFoldLoadInst(LI, TD); return ConstantFoldLoadInst(LI, TD);
@ -675,8 +675,8 @@ Constant *llvm::ConstantFoldConstantExpression(ConstantExpr *CE,
Ops.push_back(cast<Constant>(*i)); Ops.push_back(cast<Constant>(*i));
if (CE->isCompare()) if (CE->isCompare())
return ConstantFoldCompareInstOperands(CE->getPredicate(), return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1],
Ops.data(), Ops.size(), TD); TD);
return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(), return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(),
Ops.data(), Ops.size(), TD); Ops.data(), Ops.size(), TD);
} }
@ -806,8 +806,7 @@ Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
/// returns a constant expression of the specified operands. /// returns a constant expression of the specified operands.
/// ///
Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate, Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
Constant *const *Ops, Constant *Ops0, Constant *Ops1,
unsigned NumOps,
const TargetData *TD) { const TargetData *TD) {
// fold: icmp (inttoptr x), null -> icmp x, 0 // fold: icmp (inttoptr x), null -> icmp x, 0
// fold: icmp (ptrtoint x), 0 -> icmp x, null // fold: icmp (ptrtoint x), 0 -> icmp x, null
@ -816,16 +815,16 @@ Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
// //
// ConstantExpr::getCompare cannot do this, because it doesn't have TD // ConstantExpr::getCompare cannot do this, because it doesn't have TD
// around to know if bit truncation is happening. // around to know if bit truncation is happening.
if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops[0])) { if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
if (TD && Ops[1]->isNullValue()) { if (TD && Ops1->isNullValue()) {
const Type *IntPtrTy = TD->getIntPtrType(CE0->getContext()); const Type *IntPtrTy = TD->getIntPtrType(CE0->getContext());
if (CE0->getOpcode() == Instruction::IntToPtr) { if (CE0->getOpcode() == Instruction::IntToPtr) {
// Convert the integer value to the right size to ensure we get the // Convert the integer value to the right size to ensure we get the
// proper extension or truncation. // proper extension or truncation.
Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0), Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0),
IntPtrTy, false); IntPtrTy, false);
Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) }; Constant *Null = Constant::getNullValue(C->getType());
return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, TD); return ConstantFoldCompareInstOperands(Predicate, C, Null, TD);
} }
// Only do this transformation if the int is intptrty in size, otherwise // Only do this transformation if the int is intptrty in size, otherwise
@ -833,13 +832,12 @@ Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
if (CE0->getOpcode() == Instruction::PtrToInt && if (CE0->getOpcode() == Instruction::PtrToInt &&
CE0->getType() == IntPtrTy) { CE0->getType() == IntPtrTy) {
Constant *C = CE0->getOperand(0); Constant *C = CE0->getOperand(0);
Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) }; Constant *Null = Constant::getNullValue(C->getType());
// FIXME! return ConstantFoldCompareInstOperands(Predicate, C, Null, TD);
return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, TD);
} }
} }
if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops[1])) { if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
if (TD && CE0->getOpcode() == CE1->getOpcode()) { if (TD && CE0->getOpcode() == CE1->getOpcode()) {
const Type *IntPtrTy = TD->getIntPtrType(CE0->getContext()); const Type *IntPtrTy = TD->getIntPtrType(CE0->getContext());
@ -850,24 +848,21 @@ Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate,
IntPtrTy, false); IntPtrTy, false);
Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0), Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0),
IntPtrTy, false); IntPtrTy, false);
Constant *NewOps[] = { C0, C1 }; return ConstantFoldCompareInstOperands(Predicate, C0, C1, TD);
return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, TD);
} }
// Only do this transformation if the int is intptrty in size, otherwise // Only do this transformation if the int is intptrty in size, otherwise
// there is a truncation or extension that we aren't modeling. // there is a truncation or extension that we aren't modeling.
if ((CE0->getOpcode() == Instruction::PtrToInt && if ((CE0->getOpcode() == Instruction::PtrToInt &&
CE0->getType() == IntPtrTy && CE0->getType() == IntPtrTy &&
CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType())) { CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()))
Constant *NewOps[] = { return ConstantFoldCompareInstOperands(Predicate, CE0->getOperand(0),
CE0->getOperand(0), CE1->getOperand(0) CE1->getOperand(0), TD);
};
return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, TD);
}
} }
} }
} }
return ConstantExpr::getCompare(Predicate, Ops[0], Ops[1]);
return ConstantExpr::getCompare(Predicate, Ops0, Ops1);
} }

View File

@ -39,10 +39,8 @@ Value *llvm::SimplifyCompare(unsigned Predicate, Value *LHS, Value *RHS,
CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
if (Constant *CLHS = dyn_cast<Constant>(LHS)) if (Constant *CLHS = dyn_cast<Constant>(LHS))
if (Constant *CRHS = dyn_cast<Constant>(RHS)) { if (Constant *CRHS = dyn_cast<Constant>(RHS))
Constant *COps[] = {CLHS, CRHS}; return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
return ConstantFoldCompareInstOperands(Pred, COps, 2, TD);
}
// If this is an integer compare and the LHS and RHS are the same, fold it. // If this is an integer compare and the LHS and RHS are the same, fold it.
if (LHS == RHS) if (LHS == RHS)

View File

@ -3826,11 +3826,10 @@ static Constant *EvaluateExpression(Value *V, Constant *PHIVal) {
} }
if (const CmpInst *CI = dyn_cast<CmpInst>(I)) if (const CmpInst *CI = dyn_cast<CmpInst>(I))
return ConstantFoldCompareInstOperands(CI->getPredicate(), return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0],
&Operands[0], Operands.size()); Operands[1]);
else return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
return ConstantFoldInstOperands(I->getOpcode(), I->getType(), &Operands[0], Operands.size());
&Operands[0], Operands.size());
} }
/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
@ -4037,7 +4036,7 @@ const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
Constant *C; Constant *C;
if (const CmpInst *CI = dyn_cast<CmpInst>(I)) if (const CmpInst *CI = dyn_cast<CmpInst>(I))
C = ConstantFoldCompareInstOperands(CI->getPredicate(), C = ConstantFoldCompareInstOperands(CI->getPredicate(),
&Operands[0], Operands.size()); Operands[0], Operands[1]);
else else
C = ConstantFoldInstOperands(I->getOpcode(), I->getType(), C = ConstantFoldInstOperands(I->getOpcode(), I->getType(),
&Operands[0], Operands.size()); &Operands[0], Operands.size());

View File

@ -331,8 +331,8 @@ ConstantFoldMappedInstruction(const Instruction *I) {
return 0; // All operands not constant! return 0; // All operands not constant!
if (const CmpInst *CI = dyn_cast<CmpInst>(I)) if (const CmpInst *CI = dyn_cast<CmpInst>(I))
return ConstantFoldCompareInstOperands(CI->getPredicate(), return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
&Ops[0], Ops.size(), TD); TD);
if (const LoadInst *LI = dyn_cast<LoadInst>(I)) if (const LoadInst *LI = dyn_cast<LoadInst>(I))
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0]))