mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-25 04:02:41 +01:00
Change constant folding APIs to take an optional TargetData, and change
ConstantFoldInstOperands/ConstantFoldCall to take a pointer to an array of operands + size, instead of an std::vector. In some cases, switch to using a SmallVector instead of a vector. This allows us to get rid of some special case gross code that was there to avoid the cost of constructing a vector. llvm-svn: 33670
This commit is contained in:
parent
59ba02e557
commit
659afcca15
@ -21,6 +21,7 @@
|
|||||||
#include "llvm/Support/CFG.h"
|
#include "llvm/Support/CFG.h"
|
||||||
#include "ValueMapper.h"
|
#include "ValueMapper.h"
|
||||||
#include "llvm/Transforms/Utils/Local.h"
|
#include "llvm/Transforms/Utils/Local.h"
|
||||||
|
#include "llvm/ADT/SmallVector.h"
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
// CloneBasicBlock - See comments in Cloning.h
|
// CloneBasicBlock - See comments in Cloning.h
|
||||||
@ -281,24 +282,7 @@ void PruningFunctionCloner::CloneBlock(const BasicBlock *BB) {
|
|||||||
/// mapping its operands through ValueMap if they are available.
|
/// mapping its operands through ValueMap if they are available.
|
||||||
Constant *PruningFunctionCloner::
|
Constant *PruningFunctionCloner::
|
||||||
ConstantFoldMappedInstruction(const Instruction *I) {
|
ConstantFoldMappedInstruction(const Instruction *I) {
|
||||||
if (isa<CmpInst>(I)) {
|
SmallVector<Constant*, 8> Ops;
|
||||||
if (Constant *Op0 = dyn_cast_or_null<Constant>(MapValue(I->getOperand(0),
|
|
||||||
ValueMap)))
|
|
||||||
if (Constant *Op1 = dyn_cast_or_null<Constant>(MapValue(I->getOperand(1),
|
|
||||||
ValueMap)))
|
|
||||||
return ConstantExpr::getCompare(cast<CmpInst>(I)->getPredicate(), Op0,
|
|
||||||
Op1);
|
|
||||||
return 0;
|
|
||||||
} else if (isa<BinaryOperator>(I) || isa<ShiftInst>(I)) {
|
|
||||||
if (Constant *Op0 = dyn_cast_or_null<Constant>(MapValue(I->getOperand(0),
|
|
||||||
ValueMap)))
|
|
||||||
if (Constant *Op1 = dyn_cast_or_null<Constant>(MapValue(I->getOperand(1),
|
|
||||||
ValueMap)))
|
|
||||||
return ConstantExpr::get(I->getOpcode(), Op0, Op1);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<Constant*> Ops;
|
|
||||||
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
|
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
|
||||||
if (Constant *Op = dyn_cast_or_null<Constant>(MapValue(I->getOperand(i),
|
if (Constant *Op = dyn_cast_or_null<Constant>(MapValue(I->getOperand(i),
|
||||||
ValueMap)))
|
ValueMap)))
|
||||||
@ -306,7 +290,7 @@ ConstantFoldMappedInstruction(const Instruction *I) {
|
|||||||
else
|
else
|
||||||
return 0; // All operands not constant!
|
return 0; // All operands not constant!
|
||||||
|
|
||||||
return ConstantFoldInstOperands(I, Ops);
|
return ConstantFoldInstOperands(I, &Ops[0], Ops.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto,
|
/// CloneAndPruneFunctionInto - This works exactly like CloneFunctionInto,
|
||||||
|
@ -18,8 +18,10 @@
|
|||||||
#include "llvm/Instructions.h"
|
#include "llvm/Instructions.h"
|
||||||
#include "llvm/Intrinsics.h"
|
#include "llvm/Intrinsics.h"
|
||||||
#include "llvm/Analysis/ConstantFolding.h"
|
#include "llvm/Analysis/ConstantFolding.h"
|
||||||
|
#include "llvm/Target/TargetData.h"
|
||||||
#include "llvm/Support/GetElementPtrTypeIterator.h"
|
#include "llvm/Support/GetElementPtrTypeIterator.h"
|
||||||
#include "llvm/Support/MathExtras.h"
|
#include "llvm/Support/MathExtras.h"
|
||||||
|
#include "llvm/ADT/SmallVector.h"
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
@ -30,8 +32,9 @@ using namespace llvm;
|
|||||||
/// doConstantPropagation - If an instruction references constants, try to fold
|
/// doConstantPropagation - If an instruction references constants, try to fold
|
||||||
/// them together...
|
/// them together...
|
||||||
///
|
///
|
||||||
bool llvm::doConstantPropagation(BasicBlock::iterator &II) {
|
bool llvm::doConstantPropagation(BasicBlock::iterator &II,
|
||||||
if (Constant *C = ConstantFoldInstruction(II)) {
|
const TargetData *TD) {
|
||||||
|
if (Constant *C = ConstantFoldInstruction(II, TD)) {
|
||||||
// Replaces all of the uses of a variable with uses of the constant.
|
// Replaces all of the uses of a variable with uses of the constant.
|
||||||
II->replaceAllUsesWith(C);
|
II->replaceAllUsesWith(C);
|
||||||
|
|
||||||
@ -48,7 +51,7 @@ bool llvm::doConstantPropagation(BasicBlock::iterator &II) {
|
|||||||
/// is returned. Note that this function can only fail when attempting to fold
|
/// is returned. Note that this function can only fail when attempting to fold
|
||||||
/// instructions like loads and stores, which have no constant expression form.
|
/// instructions like loads and stores, which have no constant expression form.
|
||||||
///
|
///
|
||||||
Constant *llvm::ConstantFoldInstruction(Instruction *I) {
|
Constant *llvm::ConstantFoldInstruction(Instruction *I, const TargetData *TD) {
|
||||||
if (PHINode *PN = dyn_cast<PHINode>(I)) {
|
if (PHINode *PN = dyn_cast<PHINode>(I)) {
|
||||||
if (PN->getNumIncomingValues() == 0)
|
if (PN->getNumIncomingValues() == 0)
|
||||||
return Constant::getNullValue(PN->getType());
|
return Constant::getNullValue(PN->getType());
|
||||||
@ -79,24 +82,16 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I) {
|
|||||||
case 0: return 0;
|
case 0: return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isa<BinaryOperator>(I) || isa<ShiftInst>(I)) {
|
|
||||||
return ConstantExpr::get(I->getOpcode(), Op0, Op1);
|
|
||||||
} else if (isa<ICmpInst>(I)) {
|
|
||||||
return ConstantExpr::getICmp(cast<ICmpInst>(I)->getPredicate(), Op0, Op1);
|
|
||||||
} else if (isa<FCmpInst>(I)) {
|
|
||||||
return ConstantExpr::getFCmp(cast<FCmpInst>(I)->getPredicate(), Op0, Op1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Scan the operand list, checking to see if they are all constants, if so,
|
// Scan the operand list, checking to see if they are all constants, if so,
|
||||||
// hand off to ConstantFoldInstOperands.
|
// hand off to ConstantFoldInstOperands.
|
||||||
std::vector<Constant*> Ops;
|
SmallVector<Constant*, 8> Ops;
|
||||||
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
|
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
|
||||||
if (Constant *Op = dyn_cast<Constant>(I->getOperand(i)))
|
if (Constant *Op = dyn_cast<Constant>(I->getOperand(i)))
|
||||||
Ops.push_back(Op);
|
Ops.push_back(Op);
|
||||||
else
|
else
|
||||||
return 0; // All operands not constant!
|
return 0; // All operands not constant!
|
||||||
|
|
||||||
return ConstantFoldInstOperands(I, Ops);
|
return ConstantFoldInstOperands(I, &Ops[0], Ops.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
|
/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the
|
||||||
@ -106,7 +101,8 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I) {
|
|||||||
/// constant expression form.
|
/// constant expression form.
|
||||||
///
|
///
|
||||||
Constant *llvm::ConstantFoldInstOperands(const Instruction* I,
|
Constant *llvm::ConstantFoldInstOperands(const Instruction* I,
|
||||||
const std::vector<Constant*> &Ops) {
|
Constant** Ops, unsigned NumOps,
|
||||||
|
const TargetData *TD) {
|
||||||
unsigned Opc = I->getOpcode();
|
unsigned Opc = I->getOpcode();
|
||||||
const Type *DestTy = I->getType();
|
const Type *DestTy = I->getType();
|
||||||
|
|
||||||
@ -117,12 +113,9 @@ Constant *llvm::ConstantFoldInstOperands(const Instruction* I,
|
|||||||
switch (Opc) {
|
switch (Opc) {
|
||||||
default: return 0;
|
default: return 0;
|
||||||
case Instruction::Call:
|
case Instruction::Call:
|
||||||
if (Function *F = dyn_cast<Function>(Ops[0])) {
|
if (Function *F = dyn_cast<Function>(Ops[0]))
|
||||||
if (canConstantFoldCallTo(F)) {
|
if (canConstantFoldCallTo(F))
|
||||||
std::vector<Constant*> Args(Ops.begin()+1, Ops.end());
|
return ConstantFoldCall(F, Ops+1, NumOps);
|
||||||
return ConstantFoldCall(F, Args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
case Instruction::ICmp:
|
case Instruction::ICmp:
|
||||||
case Instruction::FCmp:
|
case Instruction::FCmp:
|
||||||
@ -155,8 +148,8 @@ Constant *llvm::ConstantFoldInstOperands(const Instruction* I,
|
|||||||
return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
|
return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]);
|
||||||
case Instruction::GetElementPtr:
|
case Instruction::GetElementPtr:
|
||||||
return ConstantExpr::getGetElementPtr(Ops[0],
|
return ConstantExpr::getGetElementPtr(Ops[0],
|
||||||
std::vector<Constant*>(Ops.begin()+1,
|
std::vector<Constant*>(Ops+1,
|
||||||
Ops.end()));
|
Ops+NumOps));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user