#include "llvm/Analysis/PHITransAddr.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
static cl::opt<bool> EnableAddPhiTranslation(
"gvn-add-phi-translation", cl::init(false), cl::Hidden,
cl::desc("Enable phi-translation of add instructions"));
static bool CanPHITrans(Instruction *Inst) {
if (isa<PHINode>(Inst) ||
isa<GetElementPtrInst>(Inst))
return true;
if (isa<CastInst>(Inst) &&
isSafeToSpeculativelyExecute(Inst))
return true;
if (Inst->getOpcode() == Instruction::Add &&
isa<ConstantInt>(Inst->getOperand(1)))
return true;
return false;
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void PHITransAddr::dump() const {
if (!Addr) {
dbgs() << "PHITransAddr: null\n";
return;
}
dbgs() << "PHITransAddr: " << *Addr << "\n";
for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
dbgs() << " Input #" << i << " is " << *InstInputs[i] << "\n";
}
#endif
static bool VerifySubExpr(Value *Expr,
SmallVectorImpl<Instruction*> &InstInputs) {
Instruction *I = dyn_cast<Instruction>(Expr);
if (!I) return true;
SmallVectorImpl<Instruction *>::iterator Entry = find(InstInputs, I);
if (Entry != InstInputs.end()) {
InstInputs.erase(Entry);
return true;
}
if (!CanPHITrans(I)) {
errs() << "Instruction in PHITransAddr is not phi-translatable:\n";
errs() << *I << '\n';
llvm_unreachable("Either something is missing from InstInputs or "
"CanPHITrans is wrong.");
}
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
if (!VerifySubExpr(I->getOperand(i), InstInputs))
return false;
return true;
}
bool PHITransAddr::Verify() const {
if (!Addr) return true;
SmallVector<Instruction*, 8> Tmp(InstInputs.begin(), InstInputs.end());
if (!VerifySubExpr(Addr, Tmp))
return false;
if (!Tmp.empty()) {
errs() << "PHITransAddr contains extra instructions:\n";
for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
errs() << " InstInput #" << i << " is " << *InstInputs[i] << "\n";
llvm_unreachable("This is unexpected.");
}
return true;
}
bool PHITransAddr::IsPotentiallyPHITranslatable() const {
Instruction *Inst = dyn_cast<Instruction>(Addr);
return !Inst || CanPHITrans(Inst);
}
static void RemoveInstInputs(Value *V,
SmallVectorImpl<Instruction*> &InstInputs) {
Instruction *I = dyn_cast<Instruction>(V);
if (!I) return;
SmallVectorImpl<Instruction *>::iterator Entry = find(InstInputs, I);
if (Entry != InstInputs.end()) {
InstInputs.erase(Entry);
return;
}
assert(!isa<PHINode>(I) && "Error, removing something that isn't an input");
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
RemoveInstInputs(Op, InstInputs);
}
}
Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB,
BasicBlock *PredBB,
const DominatorTree *DT) {
Instruction *Inst = dyn_cast<Instruction>(V);
if (!Inst) return V;
bool isInput = is_contained(InstInputs, Inst);
if (isInput) {
if (Inst->getParent() != CurBB) {
return Inst;
}
InstInputs.erase(find(InstInputs, Inst));
if (PHINode *PN = dyn_cast<PHINode>(Inst))
return AddAsInput(PN->getIncomingValueForBlock(PredBB));
if (!CanPHITrans(Inst))
return nullptr;
for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
if (Instruction *Op = dyn_cast<Instruction>(Inst->getOperand(i)))
InstInputs.push_back(Op);
}
if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
if (!isSafeToSpeculativelyExecute(Cast)) return nullptr;
Value *PHIIn = PHITranslateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT);
if (!PHIIn) return nullptr;
if (PHIIn == Cast->getOperand(0))
return Cast;
if (Constant *C = dyn_cast<Constant>(PHIIn))
return AddAsInput(ConstantExpr::getCast(Cast->getOpcode(),
C, Cast->getType()));
for (User *U : PHIIn->users()) {
if (CastInst *CastI = dyn_cast<CastInst>(U))
if (CastI->getOpcode() == Cast->getOpcode() &&
CastI->getType() == Cast->getType() &&
(!DT || DT->dominates(CastI->getParent(), PredBB)))
return CastI;
}
return nullptr;
}
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
SmallVector<Value*, 8> GEPOps;
bool AnyChanged = false;
for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
Value *GEPOp = PHITranslateSubExpr(GEP->getOperand(i), CurBB, PredBB, DT);
if (!GEPOp) return nullptr;
AnyChanged |= GEPOp != GEP->getOperand(i);
GEPOps.push_back(GEPOp);
}
if (!AnyChanged)
return GEP;
if (Value *V = simplifyGEPInst(GEP->getSourceElementType(), GEPOps[0],
ArrayRef<Value *>(GEPOps).slice(1),
GEP->isInBounds(), {DL, TLI, DT, AC})) {
for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
RemoveInstInputs(GEPOps[i], InstInputs);
return AddAsInput(V);
}
Value *APHIOp = GEPOps[0];
for (User *U : APHIOp->users()) {
if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U))
if (GEPI->getType() == GEP->getType() &&
GEPI->getSourceElementType() == GEP->getSourceElementType() &&
GEPI->getNumOperands() == GEPOps.size() &&
GEPI->getParent()->getParent() == CurBB->getParent() &&
(!DT || DT->dominates(GEPI->getParent(), PredBB))) {
if (std::equal(GEPOps.begin(), GEPOps.end(), GEPI->op_begin()))
return GEPI;
}
}
return nullptr;
}
if (Inst->getOpcode() == Instruction::Add &&
isa<ConstantInt>(Inst->getOperand(1))) {
Constant *RHS = cast<ConstantInt>(Inst->getOperand(1));
bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap();
bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap();
Value *LHS = PHITranslateSubExpr(Inst->getOperand(0), CurBB, PredBB, DT);
if (!LHS) return nullptr;
if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(LHS))
if (BOp->getOpcode() == Instruction::Add)
if (ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
LHS = BOp->getOperand(0);
RHS = ConstantExpr::getAdd(RHS, CI);
isNSW = isNUW = false;
if (is_contained(InstInputs, BOp)) {
RemoveInstInputs(BOp, InstInputs);
AddAsInput(LHS);
}
}
if (Value *Res = simplifyAddInst(LHS, RHS, isNSW, isNUW, {DL, TLI, DT, AC})) {
RemoveInstInputs(LHS, InstInputs);
return AddAsInput(Res);
}
if (LHS == Inst->getOperand(0) && RHS == Inst->getOperand(1))
return Inst;
for (User *U : LHS->users()) {
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U))
if (BO->getOpcode() == Instruction::Add &&
BO->getOperand(0) == LHS && BO->getOperand(1) == RHS &&
BO->getParent()->getParent() == CurBB->getParent() &&
(!DT || DT->dominates(BO->getParent(), PredBB)))
return BO;
}
return nullptr;
}
return nullptr;
}
bool PHITransAddr::PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
const DominatorTree *DT,
bool MustDominate) {
assert(DT || !MustDominate);
assert(Verify() && "Invalid PHITransAddr!");
if (DT && DT->isReachableFromEntry(PredBB))
Addr =
PHITranslateSubExpr(Addr, CurBB, PredBB, MustDominate ? DT : nullptr);
else
Addr = nullptr;
assert(Verify() && "Invalid PHITransAddr!");
if (MustDominate)
if (Instruction *Inst = dyn_cast_or_null<Instruction>(Addr))
if (!DT->dominates(Inst->getParent(), PredBB))
Addr = nullptr;
return Addr == nullptr;
}
Value *PHITransAddr::
PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
const DominatorTree &DT,
SmallVectorImpl<Instruction*> &NewInsts) {
unsigned NISize = NewInsts.size();
Addr = InsertPHITranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts);
if (Addr) return Addr;
while (NewInsts.size() != NISize)
NewInsts.pop_back_val()->eraseFromParent();
return nullptr;
}
Value *PHITransAddr::
InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
BasicBlock *PredBB, const DominatorTree &DT,
SmallVectorImpl<Instruction*> &NewInsts) {
PHITransAddr Tmp(InVal, DL, AC);
if (!Tmp.PHITranslateValue(CurBB, PredBB, &DT, true))
return Tmp.getAddr();
auto *Inst = dyn_cast<Instruction>(InVal);
if (!Inst)
return nullptr;
if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
if (!isSafeToSpeculativelyExecute(Cast)) return nullptr;
Value *OpVal = InsertPHITranslatedSubExpr(Cast->getOperand(0),
CurBB, PredBB, DT, NewInsts);
if (!OpVal) return nullptr;
CastInst *New = CastInst::Create(Cast->getOpcode(), OpVal, InVal->getType(),
InVal->getName() + ".phi.trans.insert",
PredBB->getTerminator());
New->setDebugLoc(Inst->getDebugLoc());
NewInsts.push_back(New);
return New;
}
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
SmallVector<Value*, 8> GEPOps;
BasicBlock *CurBB = GEP->getParent();
for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
Value *OpVal = InsertPHITranslatedSubExpr(GEP->getOperand(i),
CurBB, PredBB, DT, NewInsts);
if (!OpVal) return nullptr;
GEPOps.push_back(OpVal);
}
GetElementPtrInst *Result = GetElementPtrInst::Create(
GEP->getSourceElementType(), GEPOps[0], makeArrayRef(GEPOps).slice(1),
InVal->getName() + ".phi.trans.insert", PredBB->getTerminator());
Result->setDebugLoc(Inst->getDebugLoc());
Result->setIsInBounds(GEP->isInBounds());
NewInsts.push_back(Result);
return Result;
}
if (EnableAddPhiTranslation && Inst->getOpcode() == Instruction::Add &&
isa<ConstantInt>(Inst->getOperand(1))) {
Value *OpVal = InsertPHITranslatedSubExpr(Inst->getOperand(0),
CurBB, PredBB, DT, NewInsts);
if (OpVal == 0) return 0;
BinaryOperator *Res = BinaryOperator::CreateAdd(OpVal, Inst->getOperand(1),
InVal->getName()+".phi.trans.insert",
PredBB->getTerminator());
Res->setHasNoSignedWrap(cast<BinaryOperator>(Inst)->hasNoSignedWrap());
Res->setHasNoUnsignedWrap(cast<BinaryOperator>(Inst)->hasNoUnsignedWrap());
NewInsts.push_back(Res);
return Res;
}
return nullptr;
}