Even more passes being LLVMContext'd.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74781 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Owen Anderson 2009-07-03 19:42:02 +00:00
parent 715029478c
commit fa5cbd6d0f
9 changed files with 235 additions and 181 deletions

View File

@ -178,7 +178,7 @@ public:
const std::vector<Constant*>& V);
Constant* getConstantVector(const std::vector<Constant*>& V);
Constant* getConstantVector(Constant* const* Vals, unsigned NumVals);
ConstantVector* getConstantVectorAllOnes(const VectorType* Ty);
ConstantVector* getConstantVectorAllOnesValue(const VectorType* Ty);
// MDNode accessors
MDNode* getMDNode(Value* const* Vals, unsigned NumVals);

View File

@ -16,6 +16,7 @@
#include "llvm/Transforms/Scalar.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/Instructions.h"
#include "llvm/LLVMContext.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/Dominators.h"
@ -35,7 +36,7 @@ STATISTIC(NumMemSetInfer, "Number of memsets inferred");
/// true for all i8 values obviously, but is also true for i32 0, i32 -1,
/// i16 0xF0F0, double 0.0 etc. If the value can't be handled with a repeated
/// byte store (e.g. i16 0x1234), return null.
static Value *isBytewiseValue(Value *V) {
static Value *isBytewiseValue(Value *V, LLVMContext* Context) {
// All byte-wide stores are splatable, even of arbitrary variables.
if (V->getType() == Type::Int8Ty) return V;
@ -43,9 +44,9 @@ static Value *isBytewiseValue(Value *V) {
// corresponding integer value is "byteable". An important case is 0.0.
if (ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
if (CFP->getType() == Type::FloatTy)
V = ConstantExpr::getBitCast(CFP, Type::Int32Ty);
V = Context->getConstantExprBitCast(CFP, Type::Int32Ty);
if (CFP->getType() == Type::DoubleTy)
V = ConstantExpr::getBitCast(CFP, Type::Int64Ty);
V = Context->getConstantExprBitCast(CFP, Type::Int64Ty);
// Don't handle long double formats, which have strange constraints.
}
@ -68,7 +69,7 @@ static Value *isBytewiseValue(Value *V) {
if (Val != Val2)
return 0;
}
return ConstantInt::get(Val);
return Context->getConstantInt(Val);
}
}
@ -345,7 +346,7 @@ bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator& BBI) {
// Ensure that the value being stored is something that can be memset'able a
// byte at a time like "0" or "-1" or any width, as well as things like
// 0xA0A0A0A0 and 0.0.
Value *ByteVal = isBytewiseValue(SI->getOperand(0));
Value *ByteVal = isBytewiseValue(SI->getOperand(0), Context);
if (!ByteVal)
return false;
@ -384,7 +385,7 @@ bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator& BBI) {
if (NextStore->isVolatile()) break;
// Check to see if this stored value is of the same byte-splattable value.
if (ByteVal != isBytewiseValue(NextStore->getOperand(0)))
if (ByteVal != isBytewiseValue(NextStore->getOperand(0), Context))
break;
// Check to see if this store is to a constant offset from the start ptr.
@ -438,15 +439,15 @@ bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator& BBI) {
StartPtr = Range.StartPtr;
// Cast the start ptr to be i8* as memset requires.
const Type *i8Ptr = PointerType::getUnqual(Type::Int8Ty);
const Type *i8Ptr = Context->getPointerTypeUnqual(Type::Int8Ty);
if (StartPtr->getType() != i8Ptr)
StartPtr = new BitCastInst(StartPtr, i8Ptr, StartPtr->getNameStart(),
InsertPt);
Value *Ops[] = {
StartPtr, ByteVal, // Start, value
ConstantInt::get(Type::Int64Ty, Range.End-Range.Start), // size
ConstantInt::get(Type::Int32Ty, Range.Alignment) // align
Context->getConstantInt(Type::Int64Ty, Range.End-Range.Start), // size
Context->getConstantInt(Type::Int32Ty, Range.Alignment) // align
};
Value *C = CallInst::Create(MemSetF, Ops, Ops+4, "", InsertPt);
DEBUG(cerr << "Replace stores:\n";

View File

@ -27,6 +27,7 @@
#include "llvm/Function.h"
#include "llvm/Instructions.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/LLVMContext.h"
#include "llvm/Pass.h"
#include "llvm/Assembly/Writer.h"
#include "llvm/Support/CFG.h"
@ -198,8 +199,9 @@ static BinaryOperator *isReassociableOp(Value *V, unsigned Opcode) {
/// LowerNegateToMultiply - Replace 0-X with X*-1.
///
static Instruction *LowerNegateToMultiply(Instruction *Neg,
std::map<AssertingVH<>, unsigned> &ValueRankMap) {
Constant *Cst = ConstantInt::getAllOnesValue(Neg->getType());
std::map<AssertingVH<>, unsigned> &ValueRankMap,
LLVMContext* Context) {
Constant *Cst = Context->getConstantIntAllOnesValue(Neg->getType());
Instruction *Res = BinaryOperator::CreateMul(Neg->getOperand(1), Cst, "",Neg);
ValueRankMap.erase(Neg);
@ -263,11 +265,13 @@ void Reassociate::LinearizeExprTree(BinaryOperator *I,
// transform them into multiplies by -1 so they can be reassociated.
if (I->getOpcode() == Instruction::Mul) {
if (!LHSBO && LHS->hasOneUse() && BinaryOperator::isNeg(LHS)) {
LHS = LowerNegateToMultiply(cast<Instruction>(LHS), ValueRankMap);
LHS = LowerNegateToMultiply(cast<Instruction>(LHS),
ValueRankMap, Context);
LHSBO = isReassociableOp(LHS, Opcode);
}
if (!RHSBO && RHS->hasOneUse() && BinaryOperator::isNeg(RHS)) {
RHS = LowerNegateToMultiply(cast<Instruction>(RHS), ValueRankMap);
RHS = LowerNegateToMultiply(cast<Instruction>(RHS),
ValueRankMap, Context);
RHSBO = isReassociableOp(RHS, Opcode);
}
}
@ -280,8 +284,8 @@ void Reassociate::LinearizeExprTree(BinaryOperator *I,
Ops.push_back(ValueEntry(getRank(RHS), RHS));
// Clear the leaves out.
I->setOperand(0, UndefValue::get(I->getType()));
I->setOperand(1, UndefValue::get(I->getType()));
I->setOperand(0, Context->getUndef(I->getType()));
I->setOperand(1, Context->getUndef(I->getType()));
return;
} else {
// Turn X+(Y+Z) -> (Y+Z)+X
@ -316,7 +320,7 @@ void Reassociate::LinearizeExprTree(BinaryOperator *I,
Ops.push_back(ValueEntry(getRank(RHS), RHS));
// Clear the RHS leaf out.
I->setOperand(1, UndefValue::get(I->getType()));
I->setOperand(1, Context->getUndef(I->getType()));
}
// RewriteExprTree - Now that the operands for this expression tree are
@ -453,15 +457,17 @@ static Instruction *BreakUpSubtract(Instruction *Sub,
/// by one, change this into a multiply by a constant to assist with further
/// reassociation.
static Instruction *ConvertShiftToMul(Instruction *Shl,
std::map<AssertingVH<>, unsigned> &ValueRankMap) {
std::map<AssertingVH<>, unsigned> &ValueRankMap,
LLVMContext* Context) {
// If an operand of this shift is a reassociable multiply, or if the shift
// is used by a reassociable multiply or add, turn into a multiply.
if (isReassociableOp(Shl->getOperand(0), Instruction::Mul) ||
(Shl->hasOneUse() &&
(isReassociableOp(Shl->use_back(), Instruction::Mul) ||
isReassociableOp(Shl->use_back(), Instruction::Add)))) {
Constant *MulCst = ConstantInt::get(Shl->getType(), 1);
MulCst = ConstantExpr::getShl(MulCst, cast<Constant>(Shl->getOperand(1)));
Constant *MulCst = Context->getConstantInt(Shl->getType(), 1);
MulCst =
Context->getConstantExprShl(MulCst, cast<Constant>(Shl->getOperand(1)));
Instruction *Mul = BinaryOperator::CreateMul(Shl->getOperand(0), MulCst,
"", Shl);
@ -561,7 +567,7 @@ Value *Reassociate::OptimizeExpression(BinaryOperator *I,
if (Constant *V1 = dyn_cast<Constant>(Ops[Ops.size()-2].Op))
if (Constant *V2 = dyn_cast<Constant>(Ops.back().Op)) {
Ops.pop_back();
Ops.back().Op = ConstantExpr::get(Opcode, V1, V2);
Ops.back().Op = Context->getConstantExpr(Opcode, V1, V2);
return OptimizeExpression(I, Ops);
}
@ -617,10 +623,10 @@ Value *Reassociate::OptimizeExpression(BinaryOperator *I,
if (FoundX != i) {
if (Opcode == Instruction::And) { // ...&X&~X = 0
++NumAnnihil;
return Constant::getNullValue(X->getType());
return Context->getNullValue(X->getType());
} else if (Opcode == Instruction::Or) { // ...|X|~X = -1
++NumAnnihil;
return ConstantInt::getAllOnesValue(X->getType());
return Context->getConstantIntAllOnesValue(X->getType());
}
}
}
@ -639,7 +645,7 @@ Value *Reassociate::OptimizeExpression(BinaryOperator *I,
assert(Opcode == Instruction::Xor);
if (e == 2) {
++NumAnnihil;
return Constant::getNullValue(Ops[0].Op->getType());
return Context->getNullValue(Ops[0].Op->getType());
}
// ... X^X -> ...
Ops.erase(Ops.begin()+i, Ops.begin()+i+2);
@ -664,7 +670,7 @@ Value *Reassociate::OptimizeExpression(BinaryOperator *I,
// Remove X and -X from the operand list.
if (Ops.size() == 2) {
++NumAnnihil;
return Constant::getNullValue(X->getType());
return Context->getNullValue(X->getType());
} else {
Ops.erase(Ops.begin()+i);
if (i < FoundX)
@ -779,7 +785,7 @@ void Reassociate::ReassociateBB(BasicBlock *BB) {
Instruction *BI = BBI++;
if (BI->getOpcode() == Instruction::Shl &&
isa<ConstantInt>(BI->getOperand(1)))
if (Instruction *NI = ConvertShiftToMul(BI, ValueRankMap)) {
if (Instruction *NI = ConvertShiftToMul(BI, ValueRankMap, Context)) {
MadeChange = true;
BI = NI;
}
@ -801,7 +807,7 @@ void Reassociate::ReassociateBB(BasicBlock *BB) {
if (isReassociableOp(BI->getOperand(1), Instruction::Mul) &&
(!BI->hasOneUse() ||
!isReassociableOp(BI->use_back(), Instruction::Mul))) {
BI = LowerNegateToMultiply(BI, ValueRankMap);
BI = LowerNegateToMultiply(BI, ValueRankMap, Context);
MadeChange = true;
}
}

View File

@ -21,6 +21,7 @@
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Pass.h"
#include "llvm/Function.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/BasicBlock.h"
#include "llvm/Instructions.h"
@ -68,7 +69,7 @@ namespace {
CastInst *AllocaInsertionPoint =
CastInst::Create(Instruction::BitCast,
Constant::getNullValue(Type::Int32Ty), Type::Int32Ty,
Context->getNullValue(Type::Int32Ty), Type::Int32Ty,
"reg2mem alloca point", I);
// Find the escaped instructions. But don't create stack slots for

View File

@ -27,6 +27,7 @@
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Instructions.h"
#include "llvm/LLVMContext.h"
#include "llvm/Pass.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/ValueTracking.h"
@ -138,6 +139,7 @@ public:
/// Constant Propagation.
///
class SCCPSolver : public InstVisitor<SCCPSolver> {
LLVMContext* Context;
DenseSet<BasicBlock*> BBExecutable;// The basic blocks that are executable
std::map<Value*, LatticeVal> ValueState; // The state each value is in.
@ -177,6 +179,7 @@ class SCCPSolver : public InstVisitor<SCCPSolver> {
typedef std::pair<BasicBlock*, BasicBlock*> Edge;
DenseSet<Edge> KnownFeasibleEdges;
public:
void setContext(LLVMContext* C) { Context = C; }
/// MarkBlockExecutable - This method can be used by clients to mark all of
/// the blocks that are known to be intrinsically live in the processed unit.
@ -437,7 +440,7 @@ void SCCPSolver::getFeasibleSuccessors(TerminatorInst &TI,
Succs[0] = Succs[1] = true;
} else if (BCValue.isConstant()) {
// Constant condition variables mean the branch can only go a single way
Succs[BCValue.getConstant() == ConstantInt::getFalse()] = true;
Succs[BCValue.getConstant() == Context->getConstantIntFalse()] = true;
}
}
} else if (isa<InvokeInst>(&TI)) {
@ -482,7 +485,7 @@ bool SCCPSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To) {
// Constant condition variables mean the branch can only go a single way
return BI->getSuccessor(BCValue.getConstant() ==
ConstantInt::getFalse()) == To;
Context->getConstantIntFalse()) == To;
}
return false;
}
@ -663,7 +666,7 @@ void SCCPSolver::visitCastInst(CastInst &I) {
if (VState.isOverdefined()) // Inherit overdefinedness of operand
markOverdefined(&I);
else if (VState.isConstant()) // Propagate constant value
markConstant(&I, ConstantExpr::getCast(I.getOpcode(),
markConstant(&I, Context->getConstantExprCast(I.getOpcode(),
VState.getConstant(), I.getType()));
}
@ -806,11 +809,12 @@ void SCCPSolver::visitBinaryOperator(Instruction &I) {
if (NonOverdefVal->isUndefined()) {
// Could annihilate value.
if (I.getOpcode() == Instruction::And)
markConstant(IV, &I, Constant::getNullValue(I.getType()));
markConstant(IV, &I, Context->getNullValue(I.getType()));
else if (const VectorType *PT = dyn_cast<VectorType>(I.getType()))
markConstant(IV, &I, ConstantVector::getAllOnesValue(PT));
markConstant(IV, &I, Context->getConstantVectorAllOnesValue(PT));
else
markConstant(IV, &I, ConstantInt::getAllOnesValue(I.getType()));
markConstant(IV, &I,
Context->getConstantIntAllOnesValue(I.getType()));
return;
} else {
if (I.getOpcode() == Instruction::And) {
@ -854,7 +858,8 @@ void SCCPSolver::visitBinaryOperator(Instruction &I) {
Result.markOverdefined();
break; // Cannot fold this operation over the PHI nodes!
} else if (In1.isConstant() && In2.isConstant()) {
Constant *V = ConstantExpr::get(I.getOpcode(), In1.getConstant(),
Constant *V =
Context->getConstantExpr(I.getOpcode(), In1.getConstant(),
In2.getConstant());
if (Result.isUndefined())
Result.markConstant(V);
@ -902,7 +907,8 @@ void SCCPSolver::visitBinaryOperator(Instruction &I) {
markOverdefined(IV, &I);
} else if (V1State.isConstant() && V2State.isConstant()) {
markConstant(IV, &I, ConstantExpr::get(I.getOpcode(), V1State.getConstant(),
markConstant(IV, &I,
Context->getConstantExpr(I.getOpcode(), V1State.getConstant(),
V2State.getConstant()));
}
}
@ -939,7 +945,7 @@ void SCCPSolver::visitCmpInst(CmpInst &I) {
Result.markOverdefined();
break; // Cannot fold this operation over the PHI nodes!
} else if (In1.isConstant() && In2.isConstant()) {
Constant *V = ConstantExpr::getCompare(I.getPredicate(),
Constant *V = Context->getConstantExprCompare(I.getPredicate(),
In1.getConstant(),
In2.getConstant());
if (Result.isUndefined())
@ -988,7 +994,7 @@ void SCCPSolver::visitCmpInst(CmpInst &I) {
markOverdefined(IV, &I);
} else if (V1State.isConstant() && V2State.isConstant()) {
markConstant(IV, &I, ConstantExpr::getCompare(I.getPredicate(),
markConstant(IV, &I, Context->getConstantExprCompare(I.getPredicate(),
V1State.getConstant(),
V2State.getConstant()));
}
@ -1090,7 +1096,7 @@ void SCCPSolver::visitGetElementPtrInst(GetElementPtrInst &I) {
Constant *Ptr = Operands[0];
Operands.erase(Operands.begin()); // Erase the pointer from idx list...
markConstant(IV, &I, ConstantExpr::getGetElementPtr(Ptr, &Operands[0],
markConstant(IV, &I, Context->getConstantExprGetElementPtr(Ptr, &Operands[0],
Operands.size()));
}
@ -1124,7 +1130,7 @@ void SCCPSolver::visitLoadInst(LoadInst &I) {
if (isa<ConstantPointerNull>(Ptr) &&
cast<PointerType>(Ptr->getType())->getAddressSpace() == 0) {
// load null -> null
markConstant(IV, &I, Constant::getNullValue(I.getType()));
markConstant(IV, &I, Context->getNullValue(I.getType()));
return;
}
@ -1365,21 +1371,22 @@ bool SCCPSolver::ResolvedUndefsIn(Function &F) {
// to be handled here, because we don't know whether the top part is 1's
// or 0's.
assert(Op0LV.isUndefined());
markForcedConstant(LV, I, Constant::getNullValue(ITy));
markForcedConstant(LV, I, Context->getNullValue(ITy));
return true;
case Instruction::Mul:
case Instruction::And:
// undef * X -> 0. X could be zero.
// undef & X -> 0. X could be zero.
markForcedConstant(LV, I, Constant::getNullValue(ITy));
markForcedConstant(LV, I, Context->getNullValue(ITy));
return true;
case Instruction::Or:
// undef | X -> -1. X could be -1.
if (const VectorType *PTy = dyn_cast<VectorType>(ITy))
markForcedConstant(LV, I, ConstantVector::getAllOnesValue(PTy));
markForcedConstant(LV, I,
Context->getConstantVectorAllOnesValue(PTy));
else
markForcedConstant(LV, I, ConstantInt::getAllOnesValue(ITy));
markForcedConstant(LV, I, Context->getConstantIntAllOnesValue(ITy));
return true;
case Instruction::SDiv:
@ -1392,7 +1399,7 @@ bool SCCPSolver::ResolvedUndefsIn(Function &F) {
// undef / X -> 0. X could be maxint.
// undef % X -> 0. X could be 1.
markForcedConstant(LV, I, Constant::getNullValue(ITy));
markForcedConstant(LV, I, Context->getNullValue(ITy));
return true;
case Instruction::AShr:
@ -1413,7 +1420,7 @@ bool SCCPSolver::ResolvedUndefsIn(Function &F) {
// X >> undef -> 0. X could be 0.
// X << undef -> 0. X could be 0.
markForcedConstant(LV, I, Constant::getNullValue(ITy));
markForcedConstant(LV, I, Context->getNullValue(ITy));
return true;
case Instruction::Select:
// undef ? X : Y -> X or Y. There could be commonality between X/Y.
@ -1476,7 +1483,7 @@ bool SCCPSolver::ResolvedUndefsIn(Function &F) {
// as undef, then further analysis could think the undef went another way
// leading to an inconsistent set of conclusions.
if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
BI->setCondition(ConstantInt::getFalse());
BI->setCondition(Context->getConstantIntFalse());
} else {
SwitchInst *SI = cast<SwitchInst>(TI);
SI->setCondition(SI->getCaseValue(1));
@ -1526,6 +1533,7 @@ FunctionPass *llvm::createSCCPPass() {
bool SCCP::runOnFunction(Function &F) {
DOUT << "SCCP on function '" << F.getNameStart() << "'\n";
SCCPSolver Solver;
Solver.setContext(Context);
// Mark the first block of the function as being executable.
Solver.MarkBlockExecutable(F.begin());
@ -1565,7 +1573,7 @@ bool SCCP::runOnFunction(Function &F) {
Instruction *I = Insts.back();
Insts.pop_back();
if (!I->use_empty())
I->replaceAllUsesWith(UndefValue::get(I->getType()));
I->replaceAllUsesWith(Context->getUndef(I->getType()));
BB->getInstList().erase(I);
MadeChanges = true;
++NumInstRemoved;
@ -1585,7 +1593,7 @@ bool SCCP::runOnFunction(Function &F) {
continue;
Constant *Const = IV.isConstant()
? IV.getConstant() : UndefValue::get(Inst->getType());
? IV.getConstant() : Context->getUndef(Inst->getType());
DOUT << " Constant: " << *Const << " = " << *Inst;
// Replaces all of the uses of a variable with uses of the constant.
@ -1701,7 +1709,7 @@ bool IPSCCP::runOnModule(Module &M) {
LatticeVal &IV = Values[AI];
if (IV.isConstant() || IV.isUndefined()) {
Constant *CST = IV.isConstant() ?
IV.getConstant() : UndefValue::get(AI->getType());
IV.getConstant() : Context->getUndef(AI->getType());
DOUT << "*** Arg " << *AI << " = " << *CST <<"\n";
// Replaces all of the uses of a variable with uses of the
@ -1726,7 +1734,7 @@ bool IPSCCP::runOnModule(Module &M) {
Instruction *I = Insts.back();
Insts.pop_back();
if (!I->use_empty())
I->replaceAllUsesWith(UndefValue::get(I->getType()));
I->replaceAllUsesWith(Context->getUndef(I->getType()));
BB->getInstList().erase(I);
MadeChanges = true;
++IPNumInstRemoved;
@ -1738,7 +1746,7 @@ bool IPSCCP::runOnModule(Module &M) {
TI->getSuccessor(i)->removePredecessor(BB);
}
if (!TI->use_empty())
TI->replaceAllUsesWith(UndefValue::get(TI->getType()));
TI->replaceAllUsesWith(Context->getUndef(TI->getType()));
BB->getInstList().erase(TI);
if (&*BB != &F->front())
@ -1757,7 +1765,7 @@ bool IPSCCP::runOnModule(Module &M) {
continue;
Constant *Const = IV.isConstant()
? IV.getConstant() : UndefValue::get(Inst->getType());
? IV.getConstant() : Context->getUndef(Inst->getType());
DOUT << " Constant: " << *Const << " = " << *Inst;
// Replaces all of the uses of a variable with uses of the
@ -1831,7 +1839,7 @@ bool IPSCCP::runOnModule(Module &M) {
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))
if (!isa<UndefValue>(RI->getOperand(0)))
RI->setOperand(0, UndefValue::get(F->getReturnType()));
RI->setOperand(0, Context->getUndef(F->getReturnType()));
}
// If we infered constant or undef values for globals variables, we can delete

View File

@ -27,6 +27,7 @@
#include "llvm/GlobalVariable.h"
#include "llvm/Instructions.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/LLVMContext.h"
#include "llvm/Pass.h"
#include "llvm/Analysis/Dominators.h"
#include "llvm/Target/TargetData.h"
@ -240,7 +241,8 @@ bool SROA::performScalarRepl(Function &F) {
DOUT << "Found alloca equal to global: " << *AI;
DOUT << " memcpy = " << *TheCopy;
Constant *TheSrc = cast<Constant>(TheCopy->getOperand(2));
AI->replaceAllUsesWith(ConstantExpr::getBitCast(TheSrc, AI->getType()));
AI->replaceAllUsesWith(
Context->getConstantExprBitCast(TheSrc, AI->getType()));
TheCopy->eraseFromParent(); // Don't mutate the global.
AI->eraseFromParent();
++NumGlobals;
@ -305,7 +307,7 @@ bool SROA::performScalarRepl(Function &F) {
DOUT << "CONVERT TO SCALAR INTEGER: " << *AI << "\n";
// Create and insert the integer alloca.
const Type *NewTy = IntegerType::get(AllocaSize*8);
const Type *NewTy = Context->getIntegerType(AllocaSize*8);
NewAI = new AllocaInst(NewTy, 0, "", AI->getParent()->begin());
ConvertUsesToScalar(AI, NewAI, 0);
}
@ -369,7 +371,7 @@ void SROA::DoScalarReplacement(AllocationInst *AI,
// %insert = insertvalue { i32, i32 } %insert.0, i32 %load.1, 1
// (Also works for arrays instead of structs)
if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
Value *Insert = UndefValue::get(LI->getType());
Value *Insert = Context->getUndef(LI->getType());
for (unsigned i = 0, e = ElementAllocas.size(); i != e; ++i) {
Value *Load = new LoadInst(ElementAllocas[i], "load", LI);
Insert = InsertValueInst::Create(Insert, Load, i, "insert", LI);
@ -416,7 +418,7 @@ void SROA::DoScalarReplacement(AllocationInst *AI,
// expanded itself once the worklist is rerun.
//
SmallVector<Value*, 8> NewArgs;
NewArgs.push_back(Constant::getNullValue(Type::Int32Ty));
NewArgs.push_back(Context->getNullValue(Type::Int32Ty));
NewArgs.append(GEPI->op_begin()+3, GEPI->op_end());
RepValue = GetElementPtrInst::Create(AllocaToUse, NewArgs.begin(),
NewArgs.end(), "", GEPI);
@ -529,7 +531,7 @@ void SROA::isSafeUseOfAllocation(Instruction *User, AllocationInst *AI,
// The GEP is not safe to transform if not of the form "GEP <ptr>, 0, <cst>".
if (I == E ||
I.getOperand() != Constant::getNullValue(I.getOperand()->getType())) {
I.getOperand() != Context->getNullValue(I.getOperand()->getType())) {
return MarkUnsafe(Info);
}
@ -762,7 +764,7 @@ void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *BCInst,
const Type *BytePtrTy = MI->getRawDest()->getType();
bool SROADest = MI->getRawDest() == BCInst;
Constant *Zero = Constant::getNullValue(Type::Int32Ty);
Constant *Zero = Context->getNullValue(Type::Int32Ty);
for (unsigned i = 0, e = NewElts.size(); i != e; ++i) {
// If this is a memcpy/memmove, emit a GEP of the other element address.
@ -770,7 +772,7 @@ void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *BCInst,
unsigned OtherEltAlign = MemAlignment;
if (OtherPtr) {
Value *Idx[2] = { Zero, ConstantInt::get(Type::Int32Ty, i) };
Value *Idx[2] = { Zero, Context->getConstantInt(Type::Int32Ty, i) };
OtherElt = GetElementPtrInst::Create(OtherPtr, Idx, Idx + 2,
OtherPtr->getNameStr()+"."+utostr(i),
MI);
@ -817,7 +819,7 @@ void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *BCInst,
Constant *StoreVal;
if (ConstantInt *CI = dyn_cast<ConstantInt>(MI->getOperand(2))) {
if (CI->isZero()) {
StoreVal = Constant::getNullValue(EltTy); // 0.0, null, 0, <0,0>
StoreVal = Context->getNullValue(EltTy); // 0.0, null, 0, <0,0>
} else {
// If EltTy is a vector type, get the element type.
const Type *ValTy = EltTy->getScalarType();
@ -833,18 +835,18 @@ void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *BCInst,
}
// Convert the integer value to the appropriate type.
StoreVal = ConstantInt::get(TotalVal);
StoreVal = Context->getConstantInt(TotalVal);
if (isa<PointerType>(ValTy))
StoreVal = ConstantExpr::getIntToPtr(StoreVal, ValTy);
StoreVal = Context->getConstantExprIntToPtr(StoreVal, ValTy);
else if (ValTy->isFloatingPoint())
StoreVal = ConstantExpr::getBitCast(StoreVal, ValTy);
StoreVal = Context->getConstantExprBitCast(StoreVal, ValTy);
assert(StoreVal->getType() == ValTy && "Type mismatch!");
// If the requested value was a vector constant, create it.
if (EltTy != ValTy) {
unsigned NumElts = cast<VectorType>(ValTy)->getNumElements();
SmallVector<Constant*, 16> Elts(NumElts, StoreVal);
StoreVal = ConstantVector::get(&Elts[0], NumElts);
StoreVal = Context->getConstantVector(&Elts[0], NumElts);
}
}
new StoreInst(StoreVal, EltPtr, MI);
@ -870,15 +872,15 @@ void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *BCInst,
Value *Ops[] = {
SROADest ? EltPtr : OtherElt, // Dest ptr
SROADest ? OtherElt : EltPtr, // Src ptr
ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size
ConstantInt::get(Type::Int32Ty, OtherEltAlign) // Align
Context->getConstantInt(MI->getOperand(3)->getType(), EltSize), // Size
Context->getConstantInt(Type::Int32Ty, OtherEltAlign) // Align
};
CallInst::Create(TheFn, Ops, Ops + 4, "", MI);
} else {
assert(isa<MemSetInst>(MI));
Value *Ops[] = {
EltPtr, MI->getOperand(2), // Dest, Value,
ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size
Context->getConstantInt(MI->getOperand(3)->getType(), EltSize), // Size
Zero // Align
};
CallInst::Create(TheFn, Ops, Ops + 4, "", MI);
@ -907,7 +909,8 @@ void SROA::RewriteStoreUserOfWholeAlloca(StoreInst *SI,
return;
// Handle tail padding by extending the operand
if (TD->getTypeSizeInBits(SrcVal->getType()) != AllocaSizeBits)
SrcVal = new ZExtInst(SrcVal, IntegerType::get(AllocaSizeBits), "", SI);
SrcVal = new ZExtInst(SrcVal,
Context->getIntegerType(AllocaSizeBits), "", SI);
DOUT << "PROMOTING STORE TO WHOLE ALLOCA: " << *AI << *SI;
@ -926,7 +929,7 @@ void SROA::RewriteStoreUserOfWholeAlloca(StoreInst *SI,
Value *EltVal = SrcVal;
if (Shift) {
Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift);
Value *ShiftVal = Context->getConstantInt(EltVal->getType(), Shift);
EltVal = BinaryOperator::CreateLShr(EltVal, ShiftVal,
"sroa.store.elt", SI);
}
@ -938,7 +941,8 @@ void SROA::RewriteStoreUserOfWholeAlloca(StoreInst *SI,
if (FieldSizeBits == 0) continue;
if (FieldSizeBits != AllocaSizeBits)
EltVal = new TruncInst(EltVal, IntegerType::get(FieldSizeBits), "", SI);
EltVal = new TruncInst(EltVal,
Context->getIntegerType(FieldSizeBits), "", SI);
Value *DestField = NewElts[i];
if (EltVal->getType() == FieldTy) {
// Storing to an integer field of this size, just do it.
@ -948,7 +952,7 @@ void SROA::RewriteStoreUserOfWholeAlloca(StoreInst *SI,
} else {
// Otherwise, bitcast the dest pointer (for aggregates).
DestField = new BitCastInst(DestField,
PointerType::getUnqual(EltVal->getType()),
Context->getPointerTypeUnqual(EltVal->getType()),
"", SI);
}
new StoreInst(EltVal, DestField, SI);
@ -973,14 +977,15 @@ void SROA::RewriteStoreUserOfWholeAlloca(StoreInst *SI,
Value *EltVal = SrcVal;
if (Shift) {
Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift);
Value *ShiftVal = Context->getConstantInt(EltVal->getType(), Shift);
EltVal = BinaryOperator::CreateLShr(EltVal, ShiftVal,
"sroa.store.elt", SI);
}
// Truncate down to an integer of the right size.
if (ElementSizeBits != AllocaSizeBits)
EltVal = new TruncInst(EltVal, IntegerType::get(ElementSizeBits),"",SI);
EltVal = new TruncInst(EltVal,
Context->getIntegerType(ElementSizeBits),"",SI);
Value *DestField = NewElts[i];
if (EltVal->getType() == ArrayEltTy) {
// Storing to an integer field of this size, just do it.
@ -990,7 +995,7 @@ void SROA::RewriteStoreUserOfWholeAlloca(StoreInst *SI,
} else {
// Otherwise, bitcast the dest pointer (for aggregates).
DestField = new BitCastInst(DestField,
PointerType::getUnqual(EltVal->getType()),
Context->getPointerTypeUnqual(EltVal->getType()),
"", SI);
}
new StoreInst(EltVal, DestField, SI);
@ -1034,7 +1039,8 @@ void SROA::RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocationInst *AI,
ArrayEltBitOffset = TD->getTypeAllocSizeInBits(ArrayEltTy);
}
Value *ResultVal = Constant::getNullValue(IntegerType::get(AllocaSizeBits));
Value *ResultVal =
Context->getNullValue(Context->getIntegerType(AllocaSizeBits));
for (unsigned i = 0, e = NewElts.size(); i != e; ++i) {
// Load the value from the alloca. If the NewElt is an aggregate, cast
@ -1047,10 +1053,11 @@ void SROA::RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocationInst *AI,
// Ignore zero sized fields like {}, they obviously contain no data.
if (FieldSizeBits == 0) continue;
const IntegerType *FieldIntTy = IntegerType::get(FieldSizeBits);
const IntegerType *FieldIntTy = Context->getIntegerType(FieldSizeBits);
if (!isa<IntegerType>(FieldTy) && !FieldTy->isFloatingPoint() &&
!isa<VectorType>(FieldTy))
SrcField = new BitCastInst(SrcField, PointerType::getUnqual(FieldIntTy),
SrcField = new BitCastInst(SrcField,
Context->getPointerTypeUnqual(FieldIntTy),
"", LI);
SrcField = new LoadInst(SrcField, "sroa.load.elt", LI);
@ -1075,7 +1082,7 @@ void SROA::RewriteLoadUserOfWholeAlloca(LoadInst *LI, AllocationInst *AI,
Shift = AllocaSizeBits-Shift-FieldIntTy->getBitWidth();
if (Shift) {
Value *ShiftVal = ConstantInt::get(SrcField->getType(), Shift);
Value *ShiftVal = Context->getConstantInt(SrcField->getType(), Shift);
SrcField = BinaryOperator::CreateShl(SrcField, ShiftVal, "", LI);
}
@ -1179,7 +1186,7 @@ void SROA::CleanupGEP(GetElementPtrInst *GEPI) {
return;
if (NumElements == 1) {
GEPI->setOperand(2, Constant::getNullValue(Type::Int32Ty));
GEPI->setOperand(2, Context->getNullValue(Type::Int32Ty));
return;
}
@ -1187,16 +1194,16 @@ void SROA::CleanupGEP(GetElementPtrInst *GEPI) {
// All users of the GEP must be loads. At each use of the GEP, insert
// two loads of the appropriate indexed GEP and select between them.
Value *IsOne = new ICmpInst(ICmpInst::ICMP_NE, I.getOperand(),
Constant::getNullValue(I.getOperand()->getType()),
Context->getNullValue(I.getOperand()->getType()),
"isone", GEPI);
// Insert the new GEP instructions, which are properly indexed.
SmallVector<Value*, 8> Indices(GEPI->op_begin()+1, GEPI->op_end());
Indices[1] = Constant::getNullValue(Type::Int32Ty);
Indices[1] = Context->getNullValue(Type::Int32Ty);
Value *ZeroIdx = GetElementPtrInst::Create(GEPI->getOperand(0),
Indices.begin(),
Indices.end(),
GEPI->getName()+".0", GEPI);
Indices[1] = ConstantInt::get(Type::Int32Ty, 1);
Indices[1] = Context->getConstantInt(Type::Int32Ty, 1);
Value *OneIdx = GetElementPtrInst::Create(GEPI->getOperand(0),
Indices.begin(),
Indices.end(),
@ -1253,7 +1260,8 @@ void SROA::CleanupAllocaUsers(AllocationInst *AI) {
/// large) integer type with extract and insert operations where the loads
/// and stores would mutate the memory.
static void MergeInType(const Type *In, uint64_t Offset, const Type *&VecTy,
unsigned AllocaSize, const TargetData &TD) {
unsigned AllocaSize, const TargetData &TD,
LLVMContext* Context) {
// If this could be contributing to a vector, analyze it.
if (VecTy != Type::VoidTy) { // either null or a vector type.
@ -1281,7 +1289,7 @@ static void MergeInType(const Type *In, uint64_t Offset, const Type *&VecTy,
cast<VectorType>(VecTy)->getElementType()
->getPrimitiveSizeInBits()/8 == EltSize)) {
if (VecTy == 0)
VecTy = VectorType::get(In, AllocaSize/EltSize);
VecTy = Context->getVectorType(In, AllocaSize/EltSize);
return;
}
}
@ -1312,7 +1320,7 @@ bool SROA::CanConvertToScalar(Value *V, bool &IsNotTrivial, const Type *&VecTy,
// Don't break volatile loads.
if (LI->isVolatile())
return false;
MergeInType(LI->getType(), Offset, VecTy, AllocaSize, *TD);
MergeInType(LI->getType(), Offset, VecTy, AllocaSize, *TD, Context);
SawVec |= isa<VectorType>(LI->getType());
continue;
}
@ -1320,7 +1328,8 @@ bool SROA::CanConvertToScalar(Value *V, bool &IsNotTrivial, const Type *&VecTy,
if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
// Storing the pointer, not into the value?
if (SI->getOperand(0) == V || SI->isVolatile()) return 0;
MergeInType(SI->getOperand(0)->getType(), Offset, VecTy, AllocaSize, *TD);
MergeInType(SI->getOperand(0)->getType(), Offset,
VecTy, AllocaSize, *TD, Context);
SawVec |= isa<VectorType>(SI->getOperand(0)->getType());
continue;
}
@ -1449,8 +1458,8 @@ void SROA::ConvertUsesToScalar(Value *Ptr, AllocaInst *NewAI, uint64_t Offset) {
APVal |= APVal << 8;
Value *Old = Builder.CreateLoad(NewAI, (NewAI->getName()+".in").c_str());
Value *New = ConvertScalar_InsertValue(ConstantInt::get(APVal), Old,
Offset, Builder);
Value *New = ConvertScalar_InsertValue(Context->getConstantInt(APVal),
Old, Offset, Builder);
Builder.CreateStore(New, NewAI);
}
MSI->eraseFromParent();
@ -1537,7 +1546,7 @@ Value *SROA::ConvertScalar_ExtractValue(Value *FromVal, const Type *ToType,
}
// Return the element extracted out of it.
Value *V = Builder.CreateExtractElement(FromVal,
ConstantInt::get(Type::Int32Ty,Elt),
Context->getConstantInt(Type::Int32Ty,Elt),
"tmp");
if (V->getType() != ToType)
V = Builder.CreateBitCast(V, ToType, "tmp");
@ -1548,7 +1557,7 @@ Value *SROA::ConvertScalar_ExtractValue(Value *FromVal, const Type *ToType,
// use insertvalue's to form the FCA.
if (const StructType *ST = dyn_cast<StructType>(ToType)) {
const StructLayout &Layout = *TD->getStructLayout(ST);
Value *Res = UndefValue::get(ST);
Value *Res = Context->getUndef(ST);
for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
Value *Elt = ConvertScalar_ExtractValue(FromVal, ST->getElementType(i),
Offset+Layout.getElementOffsetInBits(i),
@ -1560,7 +1569,7 @@ Value *SROA::ConvertScalar_ExtractValue(Value *FromVal, const Type *ToType,
if (const ArrayType *AT = dyn_cast<ArrayType>(ToType)) {
uint64_t EltSize = TD->getTypeAllocSizeInBits(AT->getElementType());
Value *Res = UndefValue::get(AT);
Value *Res = Context->getUndef(AT);
for (unsigned i = 0, e = AT->getNumElements(); i != e; ++i) {
Value *Elt = ConvertScalar_ExtractValue(FromVal, AT->getElementType(),
Offset+i*EltSize, Builder);
@ -1589,18 +1598,22 @@ Value *SROA::ConvertScalar_ExtractValue(Value *FromVal, const Type *ToType,
// We do this to support (f.e.) loads off the end of a structure where
// only some bits are used.
if (ShAmt > 0 && (unsigned)ShAmt < NTy->getBitWidth())
FromVal = Builder.CreateLShr(FromVal, ConstantInt::get(FromVal->getType(),
FromVal = Builder.CreateLShr(FromVal,
Context->getConstantInt(FromVal->getType(),
ShAmt), "tmp");
else if (ShAmt < 0 && (unsigned)-ShAmt < NTy->getBitWidth())
FromVal = Builder.CreateShl(FromVal, ConstantInt::get(FromVal->getType(),
FromVal = Builder.CreateShl(FromVal,
Context->getConstantInt(FromVal->getType(),
-ShAmt), "tmp");
// Finally, unconditionally truncate the integer to the right width.
unsigned LIBitWidth = TD->getTypeSizeInBits(ToType);
if (LIBitWidth < NTy->getBitWidth())
FromVal = Builder.CreateTrunc(FromVal, IntegerType::get(LIBitWidth), "tmp");
FromVal =
Builder.CreateTrunc(FromVal, Context->getIntegerType(LIBitWidth), "tmp");
else if (LIBitWidth > NTy->getBitWidth())
FromVal = Builder.CreateZExt(FromVal, IntegerType::get(LIBitWidth), "tmp");
FromVal =
Builder.CreateZExt(FromVal, Context->getIntegerType(LIBitWidth), "tmp");
// If the result is an integer, this is a trunc or bitcast.
if (isa<IntegerType>(ToType)) {
@ -1651,7 +1664,7 @@ Value *SROA::ConvertScalar_InsertValue(Value *SV, Value *Old,
SV = Builder.CreateBitCast(SV, VTy->getElementType(), "tmp");
SV = Builder.CreateInsertElement(Old, SV,
ConstantInt::get(Type::Int32Ty, Elt),
Context->getConstantInt(Type::Int32Ty, Elt),
"tmp");
return SV;
}
@ -1684,7 +1697,7 @@ Value *SROA::ConvertScalar_InsertValue(Value *SV, Value *Old,
unsigned SrcStoreWidth = TD->getTypeStoreSizeInBits(SV->getType());
unsigned DestStoreWidth = TD->getTypeStoreSizeInBits(AllocaType);
if (SV->getType()->isFloatingPoint() || isa<VectorType>(SV->getType()))
SV = Builder.CreateBitCast(SV, IntegerType::get(SrcWidth), "tmp");
SV = Builder.CreateBitCast(SV, Context->getIntegerType(SrcWidth), "tmp");
else if (isa<PointerType>(SV->getType()))
SV = Builder.CreatePtrToInt(SV, TD->getIntPtrType(), "tmp");
@ -1719,10 +1732,12 @@ Value *SROA::ConvertScalar_InsertValue(Value *SV, Value *Old,
// only some bits in the structure are set.
APInt Mask(APInt::getLowBitsSet(DestWidth, SrcWidth));
if (ShAmt > 0 && (unsigned)ShAmt < DestWidth) {
SV = Builder.CreateShl(SV, ConstantInt::get(SV->getType(), ShAmt), "tmp");
SV = Builder.CreateShl(SV, Context->getConstantInt(SV->getType(),
ShAmt), "tmp");
Mask <<= ShAmt;
} else if (ShAmt < 0 && (unsigned)-ShAmt < DestWidth) {
SV = Builder.CreateLShr(SV, ConstantInt::get(SV->getType(), -ShAmt), "tmp");
SV = Builder.CreateLShr(SV, Context->getConstantInt(SV->getType(),
-ShAmt), "tmp");
Mask = Mask.lshr(-ShAmt);
}
@ -1730,7 +1745,7 @@ Value *SROA::ConvertScalar_InsertValue(Value *SV, Value *Old,
// in the new bits.
if (SrcWidth != DestWidth) {
assert(DestWidth > SrcWidth);
Old = Builder.CreateAnd(Old, ConstantInt::get(~Mask), "mask");
Old = Builder.CreateAnd(Old, Context->getConstantInt(~Mask), "mask");
SV = Builder.CreateOr(Old, SV, "ins");
}
return SV;

View File

@ -26,6 +26,7 @@
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Constants.h"
#include "llvm/Instructions.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Attributes.h"
#include "llvm/Support/CFG.h"
@ -57,7 +58,7 @@ FunctionPass *llvm::createCFGSimplificationPass() {
/// ChangeToUnreachable - Insert an unreachable instruction before the specified
/// instruction, making it and the rest of the code in the block dead.
static void ChangeToUnreachable(Instruction *I) {
static void ChangeToUnreachable(Instruction *I, LLVMContext* Context) {
BasicBlock *BB = I->getParent();
// Loop over all of the successors, removing BB's entry from any PHI
// nodes.
@ -70,7 +71,7 @@ static void ChangeToUnreachable(Instruction *I) {
BasicBlock::iterator BBI = I, BBE = BB->end();
while (BBI != BBE) {
if (!BBI->use_empty())
BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
BBI->replaceAllUsesWith(Context->getUndef(BBI->getType()));
BB->getInstList().erase(BBI++);
}
}
@ -95,7 +96,8 @@ static void ChangeToCall(InvokeInst *II) {
}
static bool MarkAliveBlocks(BasicBlock *BB,
SmallPtrSet<BasicBlock*, 128> &Reachable) {
SmallPtrSet<BasicBlock*, 128> &Reachable,
LLVMContext* Context) {
SmallVector<BasicBlock*, 128> Worklist;
Worklist.push_back(BB);
@ -118,7 +120,7 @@ static bool MarkAliveBlocks(BasicBlock *BB,
// though.
++BBI;
if (!isa<UnreachableInst>(BBI)) {
ChangeToUnreachable(BBI);
ChangeToUnreachable(BBI, Context);
Changed = true;
}
break;
@ -131,7 +133,7 @@ static bool MarkAliveBlocks(BasicBlock *BB,
if (isa<UndefValue>(Ptr) ||
(isa<ConstantPointerNull>(Ptr) &&
cast<PointerType>(Ptr->getType())->getAddressSpace() == 0)) {
ChangeToUnreachable(SI);
ChangeToUnreachable(SI, Context);
Changed = true;
break;
}
@ -157,7 +159,7 @@ static bool MarkAliveBlocks(BasicBlock *BB,
/// otherwise.
static bool RemoveUnreachableBlocksFromFn(Function &F) {
SmallPtrSet<BasicBlock*, 128> Reachable;
bool Changed = MarkAliveBlocks(F.begin(), Reachable);
bool Changed = MarkAliveBlocks(F.begin(), Reachable, F.getContext());
// If there are unreachable blocks in the CFG...
if (Reachable.size() == F.size())

View File

@ -20,6 +20,7 @@
#define DEBUG_TYPE "simplify-libcalls"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Intrinsics.h"
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Support/IRBuilder.h"
@ -47,6 +48,7 @@ class VISIBILITY_HIDDEN LibCallOptimization {
protected:
Function *Caller;
const TargetData *TD;
LLVMContext* Context;
public:
LibCallOptimization() { }
virtual ~LibCallOptimization() {}
@ -62,6 +64,8 @@ public:
Value *OptimizeCall(CallInst *CI, const TargetData &TD, IRBuilder<> &B) {
Caller = CI->getParent()->getParent();
this->TD = &TD;
if (CI->getCalledFunction())
Context = CI->getCalledFunction()->getContext();
return CallOptimizer(CI->getCalledFunction(), CI, B);
}
@ -119,7 +123,8 @@ public:
/// CastToCStr - Return V if it is an i8*, otherwise cast it to i8*.
Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder<> &B) {
return B.CreateBitCast(V, PointerType::getUnqual(Type::Int8Ty), "cstr");
return
B.CreateBitCast(V, Context->getPointerTypeUnqual(Type::Int8Ty), "cstr");
}
/// EmitStrLen - Emit a call to the strlen function to the builder, for the
@ -133,7 +138,7 @@ Value *LibCallOptimization::EmitStrLen(Value *Ptr, IRBuilder<> &B) {
Constant *StrLen =M->getOrInsertFunction("strlen", AttrListPtr::get(AWI, 2),
TD->getIntPtrType(),
PointerType::getUnqual(Type::Int8Ty),
Context->getPointerTypeUnqual(Type::Int8Ty),
NULL);
CallInst *CI = B.CreateCall(StrLen, CastToCStr(Ptr, B), "strlen");
if (const Function *F = dyn_cast<Function>(StrLen->stripPointerCasts()))
@ -152,7 +157,7 @@ Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
Tys[0] = Len->getType();
Value *MemCpy = Intrinsic::getDeclaration(M, IID, Tys, 1);
return B.CreateCall4(MemCpy, CastToCStr(Dst, B), CastToCStr(Src, B), Len,
ConstantInt::get(Type::Int32Ty, Align));
Context->getConstantInt(Type::Int32Ty, Align));
}
/// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
@ -164,8 +169,8 @@ Value *LibCallOptimization::EmitMemChr(Value *Ptr, Value *Val,
AWI = AttributeWithIndex::get(~0u, Attribute::ReadOnly | Attribute::NoUnwind);
Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(&AWI, 1),
PointerType::getUnqual(Type::Int8Ty),
PointerType::getUnqual(Type::Int8Ty),
Context->getPointerTypeUnqual(Type::Int8Ty),
Context->getPointerTypeUnqual(Type::Int8Ty),
Type::Int32Ty, TD->getIntPtrType(),
NULL);
CallInst *CI = B.CreateCall3(MemChr, CastToCStr(Ptr, B), Val, Len, "memchr");
@ -188,8 +193,8 @@ Value *LibCallOptimization::EmitMemCmp(Value *Ptr1, Value *Ptr2,
Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI, 3),
Type::Int32Ty,
PointerType::getUnqual(Type::Int8Ty),
PointerType::getUnqual(Type::Int8Ty),
Context->getPointerTypeUnqual(Type::Int8Ty),
Context->getPointerTypeUnqual(Type::Int8Ty),
TD->getIntPtrType(), NULL);
CallInst *CI = B.CreateCall3(MemCmp, CastToCStr(Ptr1, B), CastToCStr(Ptr2, B),
Len, "memcmp");
@ -208,7 +213,7 @@ Value *LibCallOptimization::EmitMemSet(Value *Dst, Value *Val,
const Type *Tys[1];
Tys[0] = Len->getType();
Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1);
Value *Align = ConstantInt::get(Type::Int32Ty, 1);
Value *Align = Context->getConstantInt(Type::Int32Ty, 1);
return B.CreateCall4(MemSet, CastToCStr(Dst, B), Val, Len, Align);
}
@ -267,7 +272,7 @@ void LibCallOptimization::EmitPutS(Value *Str, IRBuilder<> &B) {
Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI, 2),
Type::Int32Ty,
PointerType::getUnqual(Type::Int8Ty),
Context->getPointerTypeUnqual(Type::Int8Ty),
NULL);
CallInst *CI = B.CreateCall(PutS, CastToCStr(Str, B), "puts");
if (const Function *F = dyn_cast<Function>(PutS->stripPointerCasts()))
@ -307,11 +312,11 @@ void LibCallOptimization::EmitFPutS(Value *Str, Value *File, IRBuilder<> &B) {
Constant *F;
if (isa<PointerType>(File->getType()))
F = M->getOrInsertFunction("fputs", AttrListPtr::get(AWI, 3), Type::Int32Ty,
PointerType::getUnqual(Type::Int8Ty),
Context->getPointerTypeUnqual(Type::Int8Ty),
File->getType(), NULL);
else
F = M->getOrInsertFunction("fputs", Type::Int32Ty,
PointerType::getUnqual(Type::Int8Ty),
Context->getPointerTypeUnqual(Type::Int8Ty),
File->getType(), NULL);
CallInst *CI = B.CreateCall2(F, CastToCStr(Str, B), File, "fputs");
@ -332,16 +337,16 @@ void LibCallOptimization::EmitFWrite(Value *Ptr, Value *Size, Value *File,
if (isa<PointerType>(File->getType()))
F = M->getOrInsertFunction("fwrite", AttrListPtr::get(AWI, 3),
TD->getIntPtrType(),
PointerType::getUnqual(Type::Int8Ty),
Context->getPointerTypeUnqual(Type::Int8Ty),
TD->getIntPtrType(), TD->getIntPtrType(),
File->getType(), NULL);
else
F = M->getOrInsertFunction("fwrite", TD->getIntPtrType(),
PointerType::getUnqual(Type::Int8Ty),
Context->getPointerTypeUnqual(Type::Int8Ty),
TD->getIntPtrType(), TD->getIntPtrType(),
File->getType(), NULL);
CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
ConstantInt::get(TD->getIntPtrType(), 1), File);
Context->getConstantInt(TD->getIntPtrType(), 1), File);
if (const Function *Fn = dyn_cast<Function>(F->stripPointerCasts()))
CI->setCallingConv(Fn->getCallingConv());
@ -540,7 +545,7 @@ struct VISIBILITY_HIDDEN StrCatOpt : public LibCallOptimization {
// Verify the "strcat" function prototype.
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 2 ||
FT->getReturnType() != PointerType::getUnqual(Type::Int8Ty) ||
FT->getReturnType() != Context->getPointerTypeUnqual(Type::Int8Ty) ||
FT->getParamType(0) != FT->getReturnType() ||
FT->getParamType(1) != FT->getReturnType())
return 0;
@ -574,7 +579,8 @@ struct VISIBILITY_HIDDEN StrCatOpt : public LibCallOptimization {
// We have enough information to now generate the memcpy call to do the
// concatenation for us. Make a memcpy to copy the nul byte with align = 1.
EmitMemCpy(CpyDst, Src, ConstantInt::get(TD->getIntPtrType(), Len+1), 1, B);
EmitMemCpy(CpyDst, Src,
Context->getConstantInt(TD->getIntPtrType(), Len+1), 1, B);
}
};
@ -586,7 +592,7 @@ struct VISIBILITY_HIDDEN StrNCatOpt : public StrCatOpt {
// Verify the "strncat" function prototype.
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 3 ||
FT->getReturnType() != PointerType::getUnqual(Type::Int8Ty) ||
FT->getReturnType() != Context->getPointerTypeUnqual(Type::Int8Ty) ||
FT->getParamType(0) != FT->getReturnType() ||
FT->getParamType(1) != FT->getReturnType() ||
!isa<IntegerType>(FT->getParamType(2)))
@ -631,7 +637,7 @@ struct VISIBILITY_HIDDEN StrChrOpt : public LibCallOptimization {
// Verify the "strchr" function prototype.
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 2 ||
FT->getReturnType() != PointerType::getUnqual(Type::Int8Ty) ||
FT->getReturnType() != Context->getPointerTypeUnqual(Type::Int8Ty) ||
FT->getParamType(0) != FT->getReturnType())
return 0;
@ -646,7 +652,7 @@ struct VISIBILITY_HIDDEN StrChrOpt : public LibCallOptimization {
return 0;
return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
ConstantInt::get(TD->getIntPtrType(), Len), B);
Context->getConstantInt(TD->getIntPtrType(), Len), B);
}
// Otherwise, the character is a constant, see if the first argument is
@ -663,7 +669,7 @@ struct VISIBILITY_HIDDEN StrChrOpt : public LibCallOptimization {
uint64_t i = 0;
while (1) {
if (i == Str.size()) // Didn't find the char. strchr returns null.
return Constant::getNullValue(CI->getType());
return Context->getNullValue(CI->getType());
// Did we find our match?
if (Str[i] == CharValue)
break;
@ -671,7 +677,7 @@ struct VISIBILITY_HIDDEN StrChrOpt : public LibCallOptimization {
}
// strchr(s+n,c) -> gep(s+n+i,c)
Value *Idx = ConstantInt::get(Type::Int64Ty, i);
Value *Idx = Context->getConstantInt(Type::Int64Ty, i);
return B.CreateGEP(SrcStr, Idx, "strchr");
}
};
@ -685,12 +691,12 @@ struct VISIBILITY_HIDDEN StrCmpOpt : public LibCallOptimization {
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 2 || FT->getReturnType() != Type::Int32Ty ||
FT->getParamType(0) != FT->getParamType(1) ||
FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty))
FT->getParamType(0) != Context->getPointerTypeUnqual(Type::Int8Ty))
return 0;
Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
if (Str1P == Str2P) // strcmp(x,x) -> 0
return ConstantInt::get(CI->getType(), 0);
return Context->getConstantInt(CI->getType(), 0);
std::string Str1, Str2;
bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
@ -704,14 +710,15 @@ struct VISIBILITY_HIDDEN StrCmpOpt : public LibCallOptimization {
// strcmp(x, y) -> cnst (if both x and y are constant strings)
if (HasStr1 && HasStr2)
return ConstantInt::get(CI->getType(), strcmp(Str1.c_str(),Str2.c_str()));
return Context->getConstantInt(CI->getType(),
strcmp(Str1.c_str(),Str2.c_str()));
// strcmp(P, "x") -> memcmp(P, "x", 2)
uint64_t Len1 = GetStringLength(Str1P);
uint64_t Len2 = GetStringLength(Str2P);
if (Len1 && Len2) {
return EmitMemCmp(Str1P, Str2P,
ConstantInt::get(TD->getIntPtrType(),
Context->getConstantInt(TD->getIntPtrType(),
std::min(Len1, Len2)), B);
}
@ -728,13 +735,13 @@ struct VISIBILITY_HIDDEN StrNCmpOpt : public LibCallOptimization {
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 3 || FT->getReturnType() != Type::Int32Ty ||
FT->getParamType(0) != FT->getParamType(1) ||
FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty) ||
FT->getParamType(0) != Context->getPointerTypeUnqual(Type::Int8Ty) ||
!isa<IntegerType>(FT->getParamType(2)))
return 0;
Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
if (Str1P == Str2P) // strncmp(x,x,n) -> 0
return ConstantInt::get(CI->getType(), 0);
return Context->getConstantInt(CI->getType(), 0);
// Get the length argument if it is constant.
uint64_t Length;
@ -744,7 +751,7 @@ struct VISIBILITY_HIDDEN StrNCmpOpt : public LibCallOptimization {
return 0;
if (Length == 0) // strncmp(x,y,0) -> 0
return ConstantInt::get(CI->getType(), 0);
return Context->getConstantInt(CI->getType(), 0);
std::string Str1, Str2;
bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
@ -758,7 +765,7 @@ struct VISIBILITY_HIDDEN StrNCmpOpt : public LibCallOptimization {
// strncmp(x, y) -> cnst (if both x and y are constant strings)
if (HasStr1 && HasStr2)
return ConstantInt::get(CI->getType(),
return Context->getConstantInt(CI->getType(),
strncmp(Str1.c_str(), Str2.c_str(), Length));
return 0;
}
@ -774,7 +781,7 @@ struct VISIBILITY_HIDDEN StrCpyOpt : public LibCallOptimization {
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
FT->getParamType(0) != FT->getParamType(1) ||
FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty))
FT->getParamType(0) != Context->getPointerTypeUnqual(Type::Int8Ty))
return 0;
Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
@ -787,7 +794,8 @@ struct VISIBILITY_HIDDEN StrCpyOpt : public LibCallOptimization {
// We have enough information to now generate the memcpy call to do the
// concatenation for us. Make a memcpy to copy the nul byte with align = 1.
EmitMemCpy(Dst, Src, ConstantInt::get(TD->getIntPtrType(), Len), 1, B);
EmitMemCpy(Dst, Src,
Context->getConstantInt(TD->getIntPtrType(), Len), 1, B);
return Dst;
}
};
@ -800,7 +808,7 @@ struct VISIBILITY_HIDDEN StrNCpyOpt : public LibCallOptimization {
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 3 || FT->getReturnType() != FT->getParamType(0) ||
FT->getParamType(0) != FT->getParamType(1) ||
FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty) ||
FT->getParamType(0) != Context->getPointerTypeUnqual(Type::Int8Ty) ||
!isa<IntegerType>(FT->getParamType(2)))
return 0;
@ -815,7 +823,7 @@ struct VISIBILITY_HIDDEN StrNCpyOpt : public LibCallOptimization {
if (SrcLen == 0) {
// strncpy(x, "", y) -> memset(x, '\0', y, 1)
EmitMemSet(Dst, ConstantInt::get(Type::Int8Ty, '\0'), LenOp, B);
EmitMemSet(Dst, Context->getConstantInt(Type::Int8Ty, '\0'), LenOp, B);
return Dst;
}
@ -831,7 +839,8 @@ struct VISIBILITY_HIDDEN StrNCpyOpt : public LibCallOptimization {
if (Len > SrcLen+1) return 0;
// strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
EmitMemCpy(Dst, Src, ConstantInt::get(TD->getIntPtrType(), Len), 1, B);
EmitMemCpy(Dst, Src,
Context->getConstantInt(TD->getIntPtrType(), Len), 1, B);
return Dst;
}
@ -844,7 +853,7 @@ struct VISIBILITY_HIDDEN StrLenOpt : public LibCallOptimization {
virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() != 1 ||
FT->getParamType(0) != PointerType::getUnqual(Type::Int8Ty) ||
FT->getParamType(0) != Context->getPointerTypeUnqual(Type::Int8Ty) ||
!isa<IntegerType>(FT->getReturnType()))
return 0;
@ -852,7 +861,7 @@ struct VISIBILITY_HIDDEN StrLenOpt : public LibCallOptimization {
// Constant folding: strlen("xyz") -> 3
if (uint64_t Len = GetStringLength(Src))
return ConstantInt::get(CI->getType(), Len-1);
return Context->getConstantInt(CI->getType(), Len-1);
// Handle strlen(p) != 0.
if (!IsOnlyUsedInZeroEqualityComparison(CI)) return 0;
@ -899,7 +908,7 @@ struct VISIBILITY_HIDDEN MemCmpOpt : public LibCallOptimization {
Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
if (LHS == RHS) // memcmp(s,s,x) -> 0
return Constant::getNullValue(CI->getType());
return Context->getNullValue(CI->getType());
// Make sure we have a constant length.
ConstantInt *LenC = dyn_cast<ConstantInt>(CI->getOperand(3));
@ -907,7 +916,7 @@ struct VISIBILITY_HIDDEN MemCmpOpt : public LibCallOptimization {
uint64_t Len = LenC->getZExtValue();
if (Len == 0) // memcmp(s1,s2,0) -> 0
return Constant::getNullValue(CI->getType());
return Context->getNullValue(CI->getType());
if (Len == 1) { // memcmp(S1,S2,1) -> *LHS - *RHS
Value *LHSV = B.CreateLoad(CastToCStr(LHS, B), "lhsv");
@ -918,7 +927,7 @@ struct VISIBILITY_HIDDEN MemCmpOpt : public LibCallOptimization {
// memcmp(S1,S2,2) != 0 -> (*(short*)LHS ^ *(short*)RHS) != 0
// memcmp(S1,S2,4) != 0 -> (*(int*)LHS ^ *(int*)RHS) != 0
if ((Len == 2 || Len == 4) && IsOnlyUsedInZeroEqualityComparison(CI)) {
const Type *PTy = PointerType::getUnqual(Len == 2 ?
const Type *PTy = Context->getPointerTypeUnqual(Len == 2 ?
Type::Int16Ty : Type::Int32Ty);
LHS = B.CreateBitCast(LHS, PTy, "tmp");
RHS = B.CreateBitCast(RHS, PTy, "tmp");
@ -971,7 +980,7 @@ struct VISIBILITY_HIDDEN MemMoveOpt : public LibCallOptimization {
Value *Dst = CastToCStr(CI->getOperand(1), B);
Value *Src = CastToCStr(CI->getOperand(2), B);
Value *Size = CI->getOperand(3);
Value *Align = ConstantInt::get(Type::Int32Ty, 1);
Value *Align = Context->getConstantInt(Type::Int32Ty, 1);
B.CreateCall4(MemMove, Dst, Src, Size, Align);
return CI->getOperand(1);
}
@ -1025,7 +1034,7 @@ struct VISIBILITY_HIDDEN PowOpt : public LibCallOptimization {
if (Op2C == 0) return 0;
if (Op2C->getValueAPF().isZero()) // pow(x, 0.0) -> 1.0
return ConstantFP::get(CI->getType(), 1.0);
return Context->getConstantFP(CI->getType(), 1.0);
if (Op2C->isExactlyValue(0.5)) {
// FIXME: This is not safe for -0.0 and -inf. This can only be done when
@ -1045,7 +1054,8 @@ struct VISIBILITY_HIDDEN PowOpt : public LibCallOptimization {
if (Op2C->isExactlyValue(2.0)) // pow(x, 2.0) -> x*x
return B.CreateFMul(Op1, Op1, "pow2");
if (Op2C->isExactlyValue(-1.0)) // pow(x, -1.0) -> 1.0/x
return B.CreateFDiv(ConstantFP::get(CI->getType(), 1.0), Op1, "powrecip");
return B.CreateFDiv(Context->getConstantFP(CI->getType(), 1.0),
Op1, "powrecip");
return 0;
}
};
@ -1083,9 +1093,9 @@ struct VISIBILITY_HIDDEN Exp2Opt : public LibCallOptimization {
else
Name = "ldexpl";
Constant *One = ConstantFP::get(APFloat(1.0f));
Constant *One = Context->getConstantFP(APFloat(1.0f));
if (Op->getType() != Type::FloatTy)
One = ConstantExpr::getFPExtend(One, Op->getType());
One = Context->getConstantExprFPExtend(One, Op->getType());
Module *M = Caller->getParent();
Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
@ -1143,8 +1153,8 @@ struct VISIBILITY_HIDDEN FFSOpt : public LibCallOptimization {
// Constant fold.
if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
if (CI->getValue() == 0) // ffs(0) -> 0.
return Constant::getNullValue(CI->getType());
return ConstantInt::get(Type::Int32Ty, // ffs(c) -> cttz(c)+1
return Context->getNullValue(CI->getType());
return Context->getConstantInt(Type::Int32Ty, // ffs(c) -> cttz(c)+1
CI->getValue().countTrailingZeros()+1);
}
@ -1153,11 +1163,11 @@ struct VISIBILITY_HIDDEN FFSOpt : public LibCallOptimization {
Value *F = Intrinsic::getDeclaration(Callee->getParent(),
Intrinsic::cttz, &ArgType, 1);
Value *V = B.CreateCall(F, Op, "cttz");
V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
V = B.CreateAdd(V, Context->getConstantInt(V->getType(), 1), "tmp");
V = B.CreateIntCast(V, Type::Int32Ty, false, "tmp");
Value *Cond = B.CreateICmpNE(Op, Constant::getNullValue(ArgType), "tmp");
return B.CreateSelect(Cond, V, ConstantInt::get(Type::Int32Ty, 0));
Value *Cond = B.CreateICmpNE(Op, Context->getNullValue(ArgType), "tmp");
return B.CreateSelect(Cond, V, Context->getConstantInt(Type::Int32Ty, 0));
}
};
@ -1174,8 +1184,10 @@ struct VISIBILITY_HIDDEN IsDigitOpt : public LibCallOptimization {
// isdigit(c) -> (c-'0') <u 10
Value *Op = CI->getOperand(1);
Op = B.CreateSub(Op, ConstantInt::get(Type::Int32Ty, '0'), "isdigittmp");
Op = B.CreateICmpULT(Op, ConstantInt::get(Type::Int32Ty, 10), "isdigit");
Op = B.CreateSub(Op, Context->getConstantInt(Type::Int32Ty, '0'),
"isdigittmp");
Op = B.CreateICmpULT(Op, Context->getConstantInt(Type::Int32Ty, 10),
"isdigit");
return B.CreateZExt(Op, CI->getType());
}
};
@ -1193,7 +1205,8 @@ struct VISIBILITY_HIDDEN IsAsciiOpt : public LibCallOptimization {
// isascii(c) -> c <u 128
Value *Op = CI->getOperand(1);
Op = B.CreateICmpULT(Op, ConstantInt::get(Type::Int32Ty, 128), "isascii");
Op = B.CreateICmpULT(Op, Context->getConstantInt(Type::Int32Ty, 128),
"isascii");
return B.CreateZExt(Op, CI->getType());
}
};
@ -1211,7 +1224,8 @@ struct VISIBILITY_HIDDEN AbsOpt : public LibCallOptimization {
// abs(x) -> x >s -1 ? x : -x
Value *Op = CI->getOperand(1);
Value *Pos = B.CreateICmpSGT(Op,ConstantInt::getAllOnesValue(Op->getType()),
Value *Pos = B.CreateICmpSGT(Op,
Context->getConstantIntAllOnesValue(Op->getType()),
"ispos");
Value *Neg = B.CreateNeg(Op, "neg");
return B.CreateSelect(Pos, Op, Neg);
@ -1231,7 +1245,8 @@ struct VISIBILITY_HIDDEN ToAsciiOpt : public LibCallOptimization {
return 0;
// isascii(c) -> c & 0x7f
return B.CreateAnd(CI->getOperand(1), ConstantInt::get(CI->getType(),0x7F));
return B.CreateAnd(CI->getOperand(1),
Context->getConstantInt(CI->getType(),0x7F));
}
};
@ -1258,12 +1273,14 @@ struct VISIBILITY_HIDDEN PrintFOpt : public LibCallOptimization {
// Empty format string -> noop.
if (FormatStr.empty()) // Tolerate printf's declared void.
return CI->use_empty() ? (Value*)CI : ConstantInt::get(CI->getType(), 0);
return CI->use_empty() ? (Value*)CI :
Context->getConstantInt(CI->getType(), 0);
// printf("x") -> putchar('x'), even for '%'.
if (FormatStr.size() == 1) {
EmitPutChar(ConstantInt::get(Type::Int32Ty, FormatStr[0]), B);
return CI->use_empty() ? (Value*)CI : ConstantInt::get(CI->getType(), 1);
EmitPutChar(Context->getConstantInt(Type::Int32Ty, FormatStr[0]), B);
return CI->use_empty() ? (Value*)CI :
Context->getConstantInt(CI->getType(), 1);
}
// printf("foo\n") --> puts("foo")
@ -1272,12 +1289,12 @@ struct VISIBILITY_HIDDEN PrintFOpt : public LibCallOptimization {
// Create a string literal with no \n on it. We expect the constant merge
// pass to be run after this pass, to merge duplicate strings.
FormatStr.erase(FormatStr.end()-1);
Constant *C = ConstantArray::get(FormatStr, true);
Constant *C = Context->getConstantArray(FormatStr, true);
C = new GlobalVariable(C->getType(), true,GlobalVariable::InternalLinkage,
C, "str", Callee->getParent());
EmitPutS(C, B);
return CI->use_empty() ? (Value*)CI :
ConstantInt::get(CI->getType(), FormatStr.size()+1);
Context->getConstantInt(CI->getType(), FormatStr.size()+1);
}
// Optimize specific format strings.
@ -1285,7 +1302,8 @@ struct VISIBILITY_HIDDEN PrintFOpt : public LibCallOptimization {
if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
isa<IntegerType>(CI->getOperand(2)->getType())) {
EmitPutChar(CI->getOperand(2), B);
return CI->use_empty() ? (Value*)CI : ConstantInt::get(CI->getType(), 1);
return CI->use_empty() ? (Value*)CI :
Context->getConstantInt(CI->getType(), 1);
}
// printf("%s\n", str) --> puts(str)
@ -1326,8 +1344,8 @@ struct VISIBILITY_HIDDEN SPrintFOpt : public LibCallOptimization {
// sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
ConstantInt::get(TD->getIntPtrType(), FormatStr.size()+1),1,B);
return ConstantInt::get(CI->getType(), FormatStr.size());
Context->getConstantInt(TD->getIntPtrType(), FormatStr.size()+1),1,B);
return Context->getConstantInt(CI->getType(), FormatStr.size());
}
// The remaining optimizations require the format string to be "%s" or "%c"
@ -1342,10 +1360,10 @@ struct VISIBILITY_HIDDEN SPrintFOpt : public LibCallOptimization {
Value *V = B.CreateTrunc(CI->getOperand(3), Type::Int8Ty, "char");
Value *Ptr = CastToCStr(CI->getOperand(1), B);
B.CreateStore(V, Ptr);
Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::Int32Ty, 1), "nul");
B.CreateStore(Constant::getNullValue(Type::Int8Ty), Ptr);
Ptr = B.CreateGEP(Ptr, Context->getConstantInt(Type::Int32Ty, 1), "nul");
B.CreateStore(Context->getNullValue(Type::Int8Ty), Ptr);
return ConstantInt::get(CI->getType(), 1);
return Context->getConstantInt(CI->getType(), 1);
}
if (FormatStr[1] == 's') {
@ -1353,7 +1371,8 @@ struct VISIBILITY_HIDDEN SPrintFOpt : public LibCallOptimization {
if (!isa<PointerType>(CI->getOperand(3)->getType())) return 0;
Value *Len = EmitStrLen(CI->getOperand(3), B);
Value *IncLen = B.CreateAdd(Len, ConstantInt::get(Len->getType(), 1),
Value *IncLen = B.CreateAdd(Len,
Context->getConstantInt(Len->getType(), 1),
"leninc");
EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
@ -1386,13 +1405,13 @@ struct VISIBILITY_HIDDEN FWriteOpt : public LibCallOptimization {
// If this is writing zero records, remove the call (it's a noop).
if (Bytes == 0)
return ConstantInt::get(CI->getType(), 0);
return Context->getConstantInt(CI->getType(), 0);
// If this is writing one byte, turn it into fputc.
if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
EmitFPutC(Char, CI->getOperand(4), B);
return ConstantInt::get(CI->getType(), 1);
return Context->getConstantInt(CI->getType(), 1);
}
return 0;
@ -1414,7 +1433,8 @@ struct VISIBILITY_HIDDEN FPutsOpt : public LibCallOptimization {
// fputs(s,F) --> fwrite(s,1,strlen(s),F)
uint64_t Len = GetStringLength(CI->getOperand(1));
if (!Len) return 0;
EmitFWrite(CI->getOperand(1), ConstantInt::get(TD->getIntPtrType(), Len-1),
EmitFWrite(CI->getOperand(1),
Context->getConstantInt(TD->getIntPtrType(), Len-1),
CI->getOperand(2), B);
return CI; // Known to have no uses (see above).
}
@ -1443,10 +1463,10 @@ struct VISIBILITY_HIDDEN FPrintFOpt : public LibCallOptimization {
if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
return 0; // We found a format specifier.
EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(),
EmitFWrite(CI->getOperand(2), Context->getConstantInt(TD->getIntPtrType(),
FormatStr.size()),
CI->getOperand(1), B);
return ConstantInt::get(CI->getType(), FormatStr.size());
return Context->getConstantInt(CI->getType(), FormatStr.size());
}
// The remaining optimizations require the format string to be "%s" or "%c"
@ -1459,7 +1479,7 @@ struct VISIBILITY_HIDDEN FPrintFOpt : public LibCallOptimization {
// fprintf(F, "%c", chr) --> *(i8*)dst = chr
if (!isa<IntegerType>(CI->getOperand(3)->getType())) return 0;
EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
return ConstantInt::get(CI->getType(), 1);
return Context->getConstantInt(CI->getType(), 1);
}
if (FormatStr[1] == 's') {

View File

@ -408,7 +408,8 @@ Constant* LLVMContext::getConstantVector(Constant* const* Vals,
return ConstantVector::get(Vals, NumVals);
}
ConstantVector* LLVMContext::getConstantVectorAllOnes(const VectorType* Ty) {
ConstantVector* LLVMContext::getConstantVectorAllOnesValue(
const VectorType* Ty) {
return ConstantVector::getAllOnesValue(Ty);
}