#include "llvm/Transforms/Scalar/SCCP.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/DomTreeUpdater.h"
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/ValueLattice.h"
#include "llvm/Analysis/ValueLatticeUtils.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/SCCPSolver.h"
#include <cassert>
#include <utility>
#include <vector>
using namespace llvm;
#define DEBUG_TYPE "sccp"
STATISTIC(NumInstRemoved, "Number of instructions removed");
STATISTIC(NumDeadBlocks , "Number of basic blocks unreachable");
STATISTIC(NumInstReplaced,
"Number of instructions replaced with (simpler) instruction");
STATISTIC(IPNumInstRemoved, "Number of instructions removed by IPSCCP");
STATISTIC(IPNumArgsElimed ,"Number of arguments constant propagated by IPSCCP");
STATISTIC(IPNumGlobalConst, "Number of globals found to be constant by IPSCCP");
STATISTIC(
IPNumInstReplaced,
"Number of instructions replaced with (simpler) instruction by IPSCCP");
static bool isConstant(const ValueLatticeElement &LV) {
return LV.isConstant() ||
(LV.isConstantRange() && LV.getConstantRange().isSingleElement());
}
static bool isOverdefined(const ValueLatticeElement &LV) {
return !LV.isUnknownOrUndef() && !isConstant(LV);
}
static bool canRemoveInstruction(Instruction *I) {
if (wouldInstructionBeTriviallyDead(I))
return true;
return isa<LoadInst>(I);
}
static bool tryToReplaceWithConstant(SCCPSolver &Solver, Value *V) {
Constant *Const = nullptr;
if (V->getType()->isStructTy()) {
std::vector<ValueLatticeElement> IVs = Solver.getStructLatticeValueFor(V);
if (llvm::any_of(IVs, isOverdefined))
return false;
std::vector<Constant *> ConstVals;
auto *ST = cast<StructType>(V->getType());
for (unsigned i = 0, e = ST->getNumElements(); i != e; ++i) {
ValueLatticeElement V = IVs[i];
ConstVals.push_back(isConstant(V)
? Solver.getConstant(V)
: UndefValue::get(ST->getElementType(i)));
}
Const = ConstantStruct::get(ST, ConstVals);
} else {
const ValueLatticeElement &IV = Solver.getLatticeValueFor(V);
if (isOverdefined(IV))
return false;
Const =
isConstant(IV) ? Solver.getConstant(IV) : UndefValue::get(V->getType());
}
assert(Const && "Constant is nullptr here!");
CallBase *CB = dyn_cast<CallBase>(V);
if (CB && ((CB->isMustTailCall() &&
!canRemoveInstruction(CB)) ||
CB->getOperandBundle(LLVMContext::OB_clang_arc_attachedcall))) {
Function *F = CB->getCalledFunction();
if (F)
Solver.addToMustPreserveReturnsInFunctions(F);
LLVM_DEBUG(dbgs() << " Can\'t treat the result of call " << *CB
<< " as a constant\n");
return false;
}
LLVM_DEBUG(dbgs() << " Constant: " << *Const << " = " << *V << '\n');
V->replaceAllUsesWith(Const);
return true;
}
static bool simplifyInstsInBlock(SCCPSolver &Solver, BasicBlock &BB,
SmallPtrSetImpl<Value *> &InsertedValues,
Statistic &InstRemovedStat,
Statistic &InstReplacedStat) {
bool MadeChanges = false;
for (Instruction &Inst : make_early_inc_range(BB)) {
if (Inst.getType()->isVoidTy())
continue;
if (tryToReplaceWithConstant(Solver, &Inst)) {
if (canRemoveInstruction(&Inst))
Inst.eraseFromParent();
MadeChanges = true;
++InstRemovedStat;
} else if (isa<SExtInst>(&Inst)) {
Value *ExtOp = Inst.getOperand(0);
if (isa<Constant>(ExtOp) || InsertedValues.count(ExtOp))
continue;
const ValueLatticeElement &IV = Solver.getLatticeValueFor(ExtOp);
if (!IV.isConstantRange(false))
continue;
if (IV.getConstantRange().isAllNonNegative()) {
auto *ZExt = new ZExtInst(ExtOp, Inst.getType(), "", &Inst);
ZExt->takeName(&Inst);
InsertedValues.insert(ZExt);
Inst.replaceAllUsesWith(ZExt);
Solver.removeLatticeValueFor(&Inst);
Inst.eraseFromParent();
InstReplacedStat++;
MadeChanges = true;
}
}
}
return MadeChanges;
}
static bool removeNonFeasibleEdges(const SCCPSolver &Solver, BasicBlock *BB,
DomTreeUpdater &DTU,
BasicBlock *&NewUnreachableBB);
static bool runSCCP(Function &F, const DataLayout &DL,
const TargetLibraryInfo *TLI, DomTreeUpdater &DTU) {
LLVM_DEBUG(dbgs() << "SCCP on function '" << F.getName() << "'\n");
SCCPSolver Solver(
DL, [TLI](Function &F) -> const TargetLibraryInfo & { return *TLI; },
F.getContext());
Solver.markBlockExecutable(&F.front());
for (Argument &AI : F.args())
Solver.markOverdefined(&AI);
bool ResolvedUndefs = true;
while (ResolvedUndefs) {
Solver.solve();
LLVM_DEBUG(dbgs() << "RESOLVING UNDEFs\n");
ResolvedUndefs = Solver.resolvedUndefsIn(F);
}
bool MadeChanges = false;
SmallPtrSet<Value *, 32> InsertedValues;
SmallVector<BasicBlock *, 8> BlocksToErase;
for (BasicBlock &BB : F) {
if (!Solver.isBlockExecutable(&BB)) {
LLVM_DEBUG(dbgs() << " BasicBlock Dead:" << BB);
++NumDeadBlocks;
BlocksToErase.push_back(&BB);
MadeChanges = true;
continue;
}
MadeChanges |= simplifyInstsInBlock(Solver, BB, InsertedValues,
NumInstRemoved, NumInstReplaced);
}
for (BasicBlock *DeadBB : BlocksToErase)
NumInstRemoved += changeToUnreachable(DeadBB->getFirstNonPHI(),
false, &DTU);
BasicBlock *NewUnreachableBB = nullptr;
for (BasicBlock &BB : F)
MadeChanges |= removeNonFeasibleEdges(Solver, &BB, DTU, NewUnreachableBB);
for (BasicBlock *DeadBB : BlocksToErase)
if (!DeadBB->hasAddressTaken())
DTU.deleteBB(DeadBB);
return MadeChanges;
}
PreservedAnalyses SCCPPass::run(Function &F, FunctionAnalysisManager &AM) {
const DataLayout &DL = F.getParent()->getDataLayout();
auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
auto *DT = AM.getCachedResult<DominatorTreeAnalysis>(F);
DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
if (!runSCCP(F, DL, &TLI, DTU))
return PreservedAnalyses::all();
auto PA = PreservedAnalyses();
PA.preserve<DominatorTreeAnalysis>();
return PA;
}
namespace {
class SCCPLegacyPass : public FunctionPass {
public:
static char ID;
SCCPLegacyPass() : FunctionPass(ID) {
initializeSCCPLegacyPassPass(*PassRegistry::getPassRegistry());
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<TargetLibraryInfoWrapperPass>();
AU.addPreserved<GlobalsAAWrapperPass>();
AU.addPreserved<DominatorTreeWrapperPass>();
}
bool runOnFunction(Function &F) override {
if (skipFunction(F))
return false;
const DataLayout &DL = F.getParent()->getDataLayout();
const TargetLibraryInfo *TLI =
&getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
DomTreeUpdater DTU(DTWP ? &DTWP->getDomTree() : nullptr,
DomTreeUpdater::UpdateStrategy::Lazy);
return runSCCP(F, DL, TLI, DTU);
}
};
}
char SCCPLegacyPass::ID = 0;
INITIALIZE_PASS_BEGIN(SCCPLegacyPass, "sccp",
"Sparse Conditional Constant Propagation", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
INITIALIZE_PASS_END(SCCPLegacyPass, "sccp",
"Sparse Conditional Constant Propagation", false, false)
FunctionPass *llvm::createSCCPPass() { return new SCCPLegacyPass(); }
static void findReturnsToZap(Function &F,
SmallVector<ReturnInst *, 8> &ReturnsToZap,
SCCPSolver &Solver) {
if (!Solver.isArgumentTrackedFunction(&F))
return;
if (Solver.mustPreserveReturn(&F)) {
LLVM_DEBUG(
dbgs()
<< "Can't zap returns of the function : " << F.getName()
<< " due to present musttail or \"clang.arc.attachedcall\" call of "
"it\n");
return;
}
assert(
all_of(F.users(),
[&Solver](User *U) {
if (isa<Instruction>(U) &&
!Solver.isBlockExecutable(cast<Instruction>(U)->getParent()))
return true;
if (!isa<CallBase>(U))
return true;
if (U->getType()->isStructTy()) {
return all_of(Solver.getStructLatticeValueFor(U),
[](const ValueLatticeElement &LV) {
return !isOverdefined(LV);
});
}
return !isOverdefined(Solver.getLatticeValueFor(U));
}) &&
"We can only zap functions where all live users have a concrete value");
for (BasicBlock &BB : F) {
if (CallInst *CI = BB.getTerminatingMustTailCall()) {
LLVM_DEBUG(dbgs() << "Can't zap return of the block due to present "
<< "musttail call : " << *CI << "\n");
(void)CI;
return;
}
if (auto *RI = dyn_cast<ReturnInst>(BB.getTerminator()))
if (!isa<UndefValue>(RI->getOperand(0)))
ReturnsToZap.push_back(RI);
}
}
static bool removeNonFeasibleEdges(const SCCPSolver &Solver, BasicBlock *BB,
DomTreeUpdater &DTU,
BasicBlock *&NewUnreachableBB) {
SmallPtrSet<BasicBlock *, 8> FeasibleSuccessors;
bool HasNonFeasibleEdges = false;
for (BasicBlock *Succ : successors(BB)) {
if (Solver.isEdgeFeasible(BB, Succ))
FeasibleSuccessors.insert(Succ);
else
HasNonFeasibleEdges = true;
}
if (!HasNonFeasibleEdges)
return false;
Instruction *TI = BB->getTerminator();
assert((isa<BranchInst>(TI) || isa<SwitchInst>(TI) ||
isa<IndirectBrInst>(TI)) &&
"Terminator must be a br, switch or indirectbr");
if (FeasibleSuccessors.size() == 0) {
SmallPtrSet<BasicBlock *, 8> SeenSuccs;
SmallVector<DominatorTree::UpdateType, 8> Updates;
for (BasicBlock *Succ : successors(BB)) {
Succ->removePredecessor(BB);
if (SeenSuccs.insert(Succ).second)
Updates.push_back({DominatorTree::Delete, BB, Succ});
}
TI->eraseFromParent();
new UnreachableInst(BB->getContext(), BB);
DTU.applyUpdatesPermissive(Updates);
} else if (FeasibleSuccessors.size() == 1) {
BasicBlock *OnlyFeasibleSuccessor = *FeasibleSuccessors.begin();
SmallVector<DominatorTree::UpdateType, 8> Updates;
bool HaveSeenOnlyFeasibleSuccessor = false;
for (BasicBlock *Succ : successors(BB)) {
if (Succ == OnlyFeasibleSuccessor && !HaveSeenOnlyFeasibleSuccessor) {
HaveSeenOnlyFeasibleSuccessor = true;
continue;
}
Succ->removePredecessor(BB);
Updates.push_back({DominatorTree::Delete, BB, Succ});
}
BranchInst::Create(OnlyFeasibleSuccessor, BB);
TI->eraseFromParent();
DTU.applyUpdatesPermissive(Updates);
} else if (FeasibleSuccessors.size() > 1) {
SwitchInstProfUpdateWrapper SI(*cast<SwitchInst>(TI));
SmallVector<DominatorTree::UpdateType, 8> Updates;
BasicBlock *DefaultDest = SI->getDefaultDest();
if (!FeasibleSuccessors.contains(DefaultDest)) {
if (!NewUnreachableBB) {
NewUnreachableBB =
BasicBlock::Create(DefaultDest->getContext(), "default.unreachable",
DefaultDest->getParent(), DefaultDest);
new UnreachableInst(DefaultDest->getContext(), NewUnreachableBB);
}
SI->setDefaultDest(NewUnreachableBB);
Updates.push_back({DominatorTree::Delete, BB, DefaultDest});
Updates.push_back({DominatorTree::Insert, BB, NewUnreachableBB});
}
for (auto CI = SI->case_begin(); CI != SI->case_end();) {
if (FeasibleSuccessors.contains(CI->getCaseSuccessor())) {
++CI;
continue;
}
BasicBlock *Succ = CI->getCaseSuccessor();
Succ->removePredecessor(BB);
Updates.push_back({DominatorTree::Delete, BB, Succ});
SI.removeCase(CI);
}
DTU.applyUpdatesPermissive(Updates);
} else {
llvm_unreachable("Must have at least one feasible successor");
}
return true;
}
bool llvm::runIPSCCP(
Module &M, const DataLayout &DL,
std::function<const TargetLibraryInfo &(Function &)> GetTLI,
function_ref<AnalysisResultsForFn(Function &)> getAnalysis) {
SCCPSolver Solver(DL, GetTLI, M.getContext());
for (Function &F : M) {
if (F.isDeclaration())
continue;
Solver.addAnalysis(F, getAnalysis(F));
if (canTrackReturnsInterprocedurally(&F))
Solver.addTrackedFunction(&F);
if (canTrackArgumentsInterprocedurally(&F)) {
Solver.addArgumentTrackedFunction(&F);
continue;
}
Solver.markBlockExecutable(&F.front());
for (Argument &AI : F.args())
Solver.markOverdefined(&AI);
}
for (GlobalVariable &G : M.globals()) {
G.removeDeadConstantUsers();
if (canTrackGlobalVariableInterprocedurally(&G))
Solver.trackValueOfGlobalVariable(&G);
}
bool ResolvedUndefs = true;
Solver.solve();
while (ResolvedUndefs) {
LLVM_DEBUG(dbgs() << "RESOLVING UNDEFS\n");
ResolvedUndefs = false;
for (Function &F : M) {
if (Solver.resolvedUndefsIn(F))
ResolvedUndefs = true;
}
if (ResolvedUndefs)
Solver.solve();
}
bool MadeChanges = false;
for (Function &F : M) {
if (F.isDeclaration())
continue;
SmallVector<BasicBlock *, 512> BlocksToErase;
if (Solver.isBlockExecutable(&F.front())) {
bool ReplacedPointerArg = false;
for (Argument &Arg : F.args()) {
if (!Arg.use_empty() && tryToReplaceWithConstant(Solver, &Arg)) {
ReplacedPointerArg |= Arg.getType()->isPointerTy();
++IPNumArgsElimed;
}
}
if (ReplacedPointerArg) {
AttributeMask AttributesToRemove;
AttributesToRemove.addAttribute(Attribute::ArgMemOnly);
AttributesToRemove.addAttribute(Attribute::InaccessibleMemOrArgMemOnly);
F.removeFnAttrs(AttributesToRemove);
for (User *U : F.users()) {
auto *CB = dyn_cast<CallBase>(U);
if (!CB || CB->getCalledFunction() != &F)
continue;
CB->removeFnAttrs(AttributesToRemove);
}
}
MadeChanges |= ReplacedPointerArg;
}
SmallPtrSet<Value *, 32> InsertedValues;
for (BasicBlock &BB : F) {
if (!Solver.isBlockExecutable(&BB)) {
LLVM_DEBUG(dbgs() << " BasicBlock Dead:" << BB);
++NumDeadBlocks;
MadeChanges = true;
if (&BB != &F.front())
BlocksToErase.push_back(&BB);
continue;
}
MadeChanges |= simplifyInstsInBlock(Solver, BB, InsertedValues,
IPNumInstRemoved, IPNumInstReplaced);
}
DomTreeUpdater DTU = Solver.getDTU(F);
for (BasicBlock *BB : BlocksToErase) {
NumInstRemoved += changeToUnreachable(BB->getFirstNonPHI(),
false, &DTU);
}
if (!Solver.isBlockExecutable(&F.front()))
NumInstRemoved += changeToUnreachable(F.front().getFirstNonPHI(),
false, &DTU);
BasicBlock *NewUnreachableBB = nullptr;
for (BasicBlock &BB : F)
MadeChanges |= removeNonFeasibleEdges(Solver, &BB, DTU, NewUnreachableBB);
for (BasicBlock *DeadBB : BlocksToErase)
if (!DeadBB->hasAddressTaken())
DTU.deleteBB(DeadBB);
for (BasicBlock &BB : F) {
for (Instruction &Inst : llvm::make_early_inc_range(BB)) {
if (Solver.getPredicateInfoFor(&Inst)) {
if (auto *II = dyn_cast<IntrinsicInst>(&Inst)) {
if (II->getIntrinsicID() == Intrinsic::ssa_copy) {
Value *Op = II->getOperand(0);
Inst.replaceAllUsesWith(Op);
Inst.eraseFromParent();
}
}
}
}
}
}
SmallVector<ReturnInst*, 8> ReturnsToZap;
for (const auto &I : Solver.getTrackedRetVals()) {
Function *F = I.first;
const ValueLatticeElement &ReturnValue = I.second;
if (ReturnValue.isConstantRange() &&
!ReturnValue.getConstantRange().isSingleElement()) {
if (ReturnValue.isConstantRangeIncludingUndef())
continue;
auto &CR = ReturnValue.getConstantRange();
for (User *User : F->users()) {
auto *CB = dyn_cast<CallBase>(User);
if (!CB || CB->getCalledFunction() != F)
continue;
if (!isGuaranteedNotToBeUndefOrPoison(CB, nullptr, CB))
continue;
if (CB->getMetadata(LLVMContext::MD_range))
continue;
LLVMContext &Context = CB->getParent()->getContext();
Metadata *RangeMD[] = {
ConstantAsMetadata::get(ConstantInt::get(Context, CR.getLower())),
ConstantAsMetadata::get(ConstantInt::get(Context, CR.getUpper()))};
CB->setMetadata(LLVMContext::MD_range, MDNode::get(Context, RangeMD));
}
continue;
}
if (F->getReturnType()->isVoidTy())
continue;
if (isConstant(ReturnValue) || ReturnValue.isUnknownOrUndef())
findReturnsToZap(*F, ReturnsToZap, Solver);
}
for (auto F : Solver.getMRVFunctionsTracked()) {
assert(F->getReturnType()->isStructTy() &&
"The return type should be a struct");
StructType *STy = cast<StructType>(F->getReturnType());
if (Solver.isStructLatticeConstant(F, STy))
findReturnsToZap(*F, ReturnsToZap, Solver);
}
SmallSetVector<Function *, 8> FuncZappedReturn;
for (unsigned i = 0, e = ReturnsToZap.size(); i != e; ++i) {
Function *F = ReturnsToZap[i]->getParent()->getParent();
ReturnsToZap[i]->setOperand(0, UndefValue::get(F->getReturnType()));
FuncZappedReturn.insert(F);
}
for (Function *F : FuncZappedReturn) {
for (Argument &A : F->args())
F->removeParamAttr(A.getArgNo(), Attribute::Returned);
for (Use &U : F->uses()) {
if (isa<BlockAddress>(U.getUser()))
continue;
CallBase *CB = cast<CallBase>(U.getUser());
for (Use &Arg : CB->args())
CB->removeParamAttr(CB->getArgOperandNo(&Arg), Attribute::Returned);
}
}
for (auto &I : make_early_inc_range(Solver.getTrackedGlobals())) {
GlobalVariable *GV = I.first;
if (isOverdefined(I.second))
continue;
LLVM_DEBUG(dbgs() << "Found that GV '" << GV->getName()
<< "' is constant!\n");
while (!GV->use_empty()) {
StoreInst *SI = cast<StoreInst>(GV->user_back());
SI->eraseFromParent();
MadeChanges = true;
}
M.getGlobalList().erase(GV);
++IPNumGlobalConst;
}
return MadeChanges;
}