1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 03:33:20 +01:00

Update to use the new MathExtras.h support for log2 computation.

Patch contributed by Jim Laskey!

llvm-svn: 22592
This commit is contained in:
Chris Lattner 2005-08-02 19:16:58 +00:00
parent eed3c76305
commit d59fba1bce

View File

@ -46,6 +46,7 @@
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/GetElementPtrTypeIterator.h" #include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/Support/InstVisitor.h" #include "llvm/Support/InstVisitor.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/PatternMatch.h" #include "llvm/Support/PatternMatch.h"
#include "llvm/ADT/DepthFirstIterator.h" #include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/Statistic.h" #include "llvm/ADT/Statistic.h"
@ -361,19 +362,6 @@ static User *dyn_castGetElementPtr(Value *V) {
return false; return false;
} }
// Log2 - Calculate the log base 2 for the specified value if it is exactly a
// power of 2.
static unsigned Log2(uint64_t Val) {
assert(Val > 1 && "Values 0 and 1 should be handled elsewhere!");
unsigned Count = 0;
while (Val != 1) {
if (Val & 1) return 0; // Multiple bits set?
Val >>= 1;
++Count;
}
return Count;
}
// AddOne, SubOne - Add or subtract a constant one from an integer constant... // AddOne, SubOne - Add or subtract a constant one from an integer constant...
static ConstantInt *AddOne(ConstantInt *C) { static ConstantInt *AddOne(ConstantInt *C) {
return cast<ConstantInt>(ConstantExpr::getAdd(C, return cast<ConstantInt>(ConstantExpr::getAdd(C,
@ -934,9 +922,11 @@ Instruction *InstCombiner::visitMul(BinaryOperator &I) {
return BinaryOperator::createNeg(Op0, I.getName()); return BinaryOperator::createNeg(Op0, I.getName());
int64_t Val = (int64_t)cast<ConstantInt>(CI)->getRawValue(); int64_t Val = (int64_t)cast<ConstantInt>(CI)->getRawValue();
if (uint64_t C = Log2(Val)) // Replace X*(2^C) with X << C if (isPowerOf2_64(Val)) { // Replace X*(2^C) with X << C
uint64_t C = Log2_64(Val);
return new ShiftInst(Instruction::Shl, Op0, return new ShiftInst(Instruction::Shl, Op0,
ConstantUInt::get(Type::UByteTy, C)); ConstantUInt::get(Type::UByteTy, C));
}
} else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) { } else if (ConstantFP *Op1F = dyn_cast<ConstantFP>(Op1)) {
if (Op1F->isNullValue()) if (Op1F->isNullValue())
return ReplaceInstUsesWith(I, Op1); return ReplaceInstUsesWith(I, Op1);
@ -1039,9 +1029,11 @@ Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
// if so, convert to a right shift. // if so, convert to a right shift.
if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS)) if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS))
if (uint64_t Val = C->getValue()) // Don't break X / 0 if (uint64_t Val = C->getValue()) // Don't break X / 0
if (uint64_t C = Log2(Val)) if (isPowerOf2_64(Val)) {
uint64_t C = Log2_64(Val);
return new ShiftInst(Instruction::Shr, Op0, return new ShiftInst(Instruction::Shr, Op0,
ConstantUInt::get(Type::UByteTy, C)); ConstantUInt::get(Type::UByteTy, C));
}
// -X/C -> X/-C // -X/C -> X/-C
if (RHS->getType()->isSigned()) if (RHS->getType()->isSigned())
@ -1072,9 +1064,8 @@ Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
} }
uint64_t TVA = STO->getValue(), FVA = SFO->getValue(); uint64_t TVA = STO->getValue(), FVA = SFO->getValue();
unsigned TSA = 0, FSA = 0; if (isPowerOf2_64(TVA) && isPowerOf2_64(FVA)) {
if ((TVA == 1 || (TSA = Log2(TVA))) && // Log2 fails for 0 & 1. unsigned TSA = Log2_64(TVA), FSA = Log2_64(FVA);
(FVA == 1 || (FSA = Log2(FVA)))) {
Constant *TC = ConstantUInt::get(Type::UByteTy, TSA); Constant *TC = ConstantUInt::get(Type::UByteTy, TSA);
Instruction *TSI = new ShiftInst(Instruction::Shr, Op0, Instruction *TSI = new ShiftInst(Instruction::Shr, Op0,
TC, SI->getName()+".t"); TC, SI->getName()+".t");
@ -2777,9 +2768,10 @@ Instruction *InstCombiner::visitSetCondInst(SetCondInst &I) {
// If we have a signed (X % (2^c)) == 0, turn it into an unsigned one. // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
if (CI->isNullValue() && isa<ConstantSInt>(BO->getOperand(1)) && if (CI->isNullValue() && isa<ConstantSInt>(BO->getOperand(1)) &&
BO->hasOneUse() && BO->hasOneUse() &&
cast<ConstantSInt>(BO->getOperand(1))->getValue() > 1) cast<ConstantSInt>(BO->getOperand(1))->getValue() > 1) {
if (unsigned L2 = int64_t V = cast<ConstantSInt>(BO->getOperand(1))->getValue();
Log2(cast<ConstantSInt>(BO->getOperand(1))->getValue())) { if (isPowerOf2_64(V)) {
unsigned L2 = Log2_64(V);
const Type *UTy = BO->getType()->getUnsignedVersion(); const Type *UTy = BO->getType()->getUnsignedVersion();
Value *NewX = InsertNewInstBefore(new CastInst(BO->getOperand(0), Value *NewX = InsertNewInstBefore(new CastInst(BO->getOperand(0),
UTy, "tmp"), I); UTy, "tmp"), I);
@ -2789,6 +2781,7 @@ Instruction *InstCombiner::visitSetCondInst(SetCondInst &I) {
return BinaryOperator::create(I.getOpcode(), NewRem, return BinaryOperator::create(I.getOpcode(), NewRem,
Constant::getNullValue(UTy)); Constant::getNullValue(UTy));
} }
}
break; break;
case Instruction::Add: case Instruction::Add:
@ -3638,7 +3631,7 @@ Instruction *InstCombiner::visitCastInst(CastInst &CI) {
if (match(Op0, m_And(m_Value(), m_ConstantInt(AndRHS)))) if (match(Op0, m_And(m_Value(), m_ConstantInt(AndRHS))))
if (AndRHS->getRawValue() && if (AndRHS->getRawValue() &&
(AndRHS->getRawValue() & (AndRHS->getRawValue()-1)) == 0) { (AndRHS->getRawValue() & (AndRHS->getRawValue()-1)) == 0) {
unsigned ShiftAmt = Log2(AndRHS->getRawValue()); unsigned ShiftAmt = Log2_64(AndRHS->getRawValue());
// Perform an unsigned shr by shiftamt. Convert input to // Perform an unsigned shr by shiftamt. Convert input to
// unsigned if it is signed. // unsigned if it is signed.
Value *In = Op0; Value *In = Op0;