#include "llvm/LLVMContext.h"
#include "llvm/Pass.h"
#include "llvm/Analysis/ConstantFolding.h"
+#include "llvm/Analysis/MallocHelper.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Support/CallSite.h"
-#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/InstVisitor.h"
/// LatticeVal class - This class represents the different lattice values that
/// an LLVM value may occupy. It is a simple class with value semantics.
///
-class VISIBILITY_HIDDEN LatticeVal {
+class LatticeVal {
enum {
/// undefined - This LLVM Value has no known value yet.
undefined,
//
inline void markConstant(LatticeVal &IV, Value *V, Constant *C) {
if (IV.markConstant(C)) {
- DEBUG(errs() << "markConstant: " << *C << ": " << *V);
+ DEBUG(errs() << "markConstant: " << *C << ": " << *V << '\n');
InstWorkList.push_back(V);
}
}
inline void markForcedConstant(LatticeVal &IV, Value *V, Constant *C) {
IV.markForcedConstant(C);
- DEBUG(errs() << "markForcedConstant: " << *C << ": " << *V);
+ DEBUG(errs() << "markForcedConstant: " << *C << ": " << *V << '\n');
InstWorkList.push_back(V);
}
if (Function *F = dyn_cast<Function>(V))
errs() << "Function '" << F->getName() << "'\n";
else
- errs() << *V);
+ errs() << *V << '\n');
// Only instructions go on the work list
OverdefinedInstWorkList.push_back(V);
}
void visitStoreInst (Instruction &I);
void visitLoadInst (LoadInst &I);
void visitGetElementPtrInst(GetElementPtrInst &I);
- void visitCallInst (CallInst &I) { visitCallSite(CallSite::get(&I)); }
+ void visitCallInst (CallInst &I) {
+ if (isMalloc(&I))
+ markOverdefined(&I);
+ else
+ visitCallSite(CallSite::get(&I));
+ }
void visitInvokeInst (InvokeInst &II) {
visitCallSite(CallSite::get(&II));
visitTerminatorInst(II);
void visitInstruction(Instruction &I) {
// If a new instruction is added to LLVM that we don't handle...
- cerr << "SCCP: Don't know how to handle: " << I;
+ errs() << "SCCP: Don't know how to handle: " << I;
markOverdefined(&I); // Just in case
}
};
return false;
} else {
#ifndef NDEBUG
- cerr << "Unknown terminator instruction: " << *TI;
+ errs() << "Unknown terminator instruction: " << *TI << '\n';
#endif
llvm_unreachable(0);
}
if (NonOverdefVal->isUndefined()) {
// Could annihilate value.
if (I.getOpcode() == Instruction::And)
- markConstant(IV, &I, Context->getNullValue(I.getType()));
+ markConstant(IV, &I, Constant::getNullValue(I.getType()));
else if (const VectorType *PT = dyn_cast<VectorType>(I.getType()))
- markConstant(IV, &I, Context->getAllOnesValue(PT));
+ markConstant(IV, &I, Constant::getAllOnesValue(PT));
else
markConstant(IV, &I,
- Context->getAllOnesValue(I.getType()));
+ Constant::getAllOnesValue(I.getType()));
return;
} else {
if (I.getOpcode() == Instruction::And) {
if (PtrVal.isConstant() && !I.isVolatile()) {
Value *Ptr = PtrVal.getConstant();
// TODO: Consider a target hook for valid address spaces for this xform.
- if (isa<ConstantPointerNull>(Ptr) &&
- cast<PointerType>(Ptr->getType())->getAddressSpace() == 0) {
+ if (isa<ConstantPointerNull>(Ptr) && I.getPointerAddressSpace() == 0) {
// load null -> null
- markConstant(IV, &I, Context->getNullValue(I.getType()));
+ markConstant(IV, &I, Constant::getNullValue(I.getType()));
return;
}
if (F == 0 || !F->hasLocalLinkage()) {
CallOverdefined:
// Void return and not tracking callee, just bail.
- if (I->getType() == Type::VoidTy) return;
+ if (I->getType() == Type::getVoidTy(I->getContext())) return;
// Otherwise, if we have a single return value case, and if the function is
// a declaration, maybe we can constant fold it.
Value *I = OverdefinedInstWorkList.back();
OverdefinedInstWorkList.pop_back();
- DEBUG(errs() << "\nPopped off OI-WL: " << *I);
+ DEBUG(errs() << "\nPopped off OI-WL: " << *I << '\n');
// "I" got into the work list because it either made the transition from
// bottom to constant
Value *I = InstWorkList.back();
InstWorkList.pop_back();
- DEBUG(errs() << "\nPopped off I-WL: " << *I);
+ DEBUG(errs() << "\nPopped off I-WL: " << *I << '\n');
// "I" got into the work list because it either made the transition from
// bottom to constant
BasicBlock *BB = BBWorkList.back();
BBWorkList.pop_back();
- DEBUG(errs() << "\nPopped off BBWL: " << *BB);
+ DEBUG(errs() << "\nPopped off BBWL: " << *BB << '\n');
// Notify all instructions in this basic block that they are newly
// executable.
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
// Look for instructions which produce undef values.
- if (I->getType() == Type::VoidTy) continue;
+ if (I->getType() == Type::getVoidTy(F.getContext())) continue;
LatticeVal &LV = getValueState(I);
if (!LV.isUndefined()) continue;
// 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, Context->getNullValue(ITy));
+ markForcedConstant(LV, I, Constant::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, Context->getNullValue(ITy));
+ markForcedConstant(LV, I, Constant::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,
- Context->getAllOnesValue(PTy));
+ Constant::getAllOnesValue(PTy));
else
- markForcedConstant(LV, I, Context->getAllOnesValue(ITy));
+ markForcedConstant(LV, I, Constant::getAllOnesValue(ITy));
return true;
case Instruction::SDiv:
// undef / X -> 0. X could be maxint.
// undef % X -> 0. X could be 1.
- markForcedConstant(LV, I, Context->getNullValue(ITy));
+ markForcedConstant(LV, I, Constant::getNullValue(ITy));
return true;
case Instruction::AShr:
// X >> undef -> 0. X could be 0.
// X << undef -> 0. X could be 0.
- markForcedConstant(LV, I, Context->getNullValue(ITy));
+ markForcedConstant(LV, I, Constant::getNullValue(ITy));
return true;
case Instruction::Select:
// undef ? X : Y -> X or Y. There could be commonality between X/Y.
/// SCCP Class - This class uses the SCCPSolver to implement a per-function
/// Sparse Conditional Constant Propagator.
///
- struct VISIBILITY_HIDDEN SCCP : public FunctionPass {
+ struct SCCP : public FunctionPass {
static char ID; // Pass identification, replacement for typeid
SCCP() : FunctionPass(&ID) {}
//
for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
Instruction *Inst = BI++;
- if (Inst->getType() == Type::VoidTy ||
+ if (Inst->getType() == Type::getVoidTy(F.getContext()) ||
isa<TerminatorInst>(Inst))
continue;
/// IPSCCP Class - This class implements interprocedural Sparse Conditional
/// Constant Propagation.
///
- struct VISIBILITY_HIDDEN IPSCCP : public ModulePass {
+ struct IPSCCP : public ModulePass {
static char ID;
IPSCCP() : ModulePass(&ID) {}
bool runOnModule(Module &M);
if (&*BB != &F->front())
BlocksToErase.push_back(BB);
else
- new UnreachableInst(BB);
+ new UnreachableInst(M.getContext(), BB);
} else {
for (BasicBlock::iterator BI = BB->begin(), E = BB->end(); BI != E; ) {
Instruction *Inst = BI++;
- if (Inst->getType() == Type::VoidTy)
+ if (Inst->getType() == Type::getVoidTy(M.getContext()))
continue;
LatticeVal &IV = Values[Inst];
for (DenseMap<Function*, LatticeVal>::const_iterator I = RV.begin(),
E = RV.end(); I != E; ++I)
if (!I->second.isOverdefined() &&
- I->first->getReturnType() != Type::VoidTy) {
+ I->first->getReturnType() != Type::getVoidTy(M.getContext())) {
Function *F = I->first;
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator()))