2009-12-04 10:10:16 +08:00
|
|
|
//===- PHITransAddr.cpp - PHI Translation for Addresses -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the PHITransAddr class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/PHITransAddr.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2011-12-15 07:49:11 +08:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2009-12-24 05:06:14 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2010-11-19 01:05:57 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-12-09 08:01:00 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-12-04 10:10:16 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
static bool CanPHITrans(Instruction *Inst) {
|
2009-12-08 02:36:53 +08:00
|
|
|
if (isa<PHINode>(Inst) ||
|
2009-12-08 03:04:49 +08:00
|
|
|
isa<GetElementPtrInst>(Inst))
|
2009-12-08 02:36:53 +08:00
|
|
|
return true;
|
2010-11-19 01:05:13 +08:00
|
|
|
|
|
|
|
if (isa<CastInst>(Inst) &&
|
2011-12-15 07:49:11 +08:00
|
|
|
isSafeToSpeculativelyExecute(Inst))
|
2010-11-19 01:05:13 +08:00
|
|
|
return true;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 14:06:26 +08:00
|
|
|
if (Inst->getOpcode() == Instruction::Add &&
|
2009-12-08 03:04:49 +08:00
|
|
|
isa<ConstantInt>(Inst->getOperand(1)))
|
|
|
|
return true;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 02:36:53 +08:00
|
|
|
// cerr << "MEMDEP: Could not PHI translate: " << *Pointer;
|
|
|
|
// if (isa<BitCastInst>(PtrInst) || isa<GetElementPtrInst>(PtrInst))
|
|
|
|
// cerr << "OP:\t\t\t\t" << *PtrInst->getOperand(0);
|
|
|
|
return false;
|
2009-12-04 10:10:16 +08:00
|
|
|
}
|
|
|
|
|
2012-09-12 13:06:18 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2009-12-09 08:01:00 +08:00
|
|
|
void PHITransAddr::dump() const {
|
2014-04-15 12:59:12 +08:00
|
|
|
if (!Addr) {
|
2009-12-24 05:06:14 +08:00
|
|
|
dbgs() << "PHITransAddr: null\n";
|
2009-12-09 08:01:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-12-24 05:06:14 +08:00
|
|
|
dbgs() << "PHITransAddr: " << *Addr << "\n";
|
2009-12-09 08:01:00 +08:00
|
|
|
for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
|
2009-12-24 05:06:14 +08:00
|
|
|
dbgs() << " Input #" << i << " is " << *InstInputs[i] << "\n";
|
2009-12-09 08:01:00 +08:00
|
|
|
}
|
2012-09-07 03:55:56 +08:00
|
|
|
#endif
|
2009-12-09 08:01:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
static bool VerifySubExpr(Value *Expr,
|
|
|
|
SmallVectorImpl<Instruction*> &InstInputs) {
|
|
|
|
// If this is a non-instruction value, there is nothing to do.
|
|
|
|
Instruction *I = dyn_cast<Instruction>(Expr);
|
2014-04-15 12:59:12 +08:00
|
|
|
if (!I) return true;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-09 08:01:00 +08:00
|
|
|
// If it's an instruction, it is either in Tmp or its operands recursively
|
|
|
|
// are.
|
|
|
|
SmallVectorImpl<Instruction*>::iterator Entry =
|
|
|
|
std::find(InstInputs.begin(), InstInputs.end(), I);
|
|
|
|
if (Entry != InstInputs.end()) {
|
|
|
|
InstInputs.erase(Entry);
|
|
|
|
return true;
|
|
|
|
}
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-09 08:01:00 +08:00
|
|
|
// If it isn't in the InstInputs list it is a subexpr incorporated into the
|
|
|
|
// address. Sanity check that it is phi translatable.
|
|
|
|
if (!CanPHITrans(I)) {
|
2013-12-05 13:44:44 +08:00
|
|
|
errs() << "Instruction in PHITransAddr is not phi-translatable:\n";
|
2009-12-24 07:27:15 +08:00
|
|
|
errs() << *I << '\n';
|
2010-11-19 01:05:57 +08:00
|
|
|
llvm_unreachable("Either something is missing from InstInputs or "
|
|
|
|
"CanPHITrans is wrong.");
|
2009-12-09 08:01:00 +08:00
|
|
|
}
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-09 08:01:00 +08:00
|
|
|
// Validate the operands of the instruction.
|
|
|
|
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
|
|
|
|
if (!VerifySubExpr(I->getOperand(i), InstInputs))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Verify - Check internal consistency of this data structure. If the
|
|
|
|
/// structure is valid, it returns true. If invalid, it prints errors and
|
|
|
|
/// returns false.
|
|
|
|
bool PHITransAddr::Verify() const {
|
2014-04-15 12:59:12 +08:00
|
|
|
if (!Addr) return true;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
|
|
|
SmallVector<Instruction*, 8> Tmp(InstInputs.begin(), InstInputs.end());
|
|
|
|
|
2009-12-09 08:01:00 +08:00
|
|
|
if (!VerifySubExpr(Addr, Tmp))
|
|
|
|
return false;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-09 08:01:00 +08:00
|
|
|
if (!Tmp.empty()) {
|
2010-11-19 01:05:57 +08:00
|
|
|
errs() << "PHITransAddr contains extra instructions:\n";
|
2009-12-09 08:01:00 +08:00
|
|
|
for (unsigned i = 0, e = InstInputs.size(); i != e; ++i)
|
2009-12-24 07:27:15 +08:00
|
|
|
errs() << " InstInput #" << i << " is " << *InstInputs[i] << "\n";
|
2010-11-19 01:05:57 +08:00
|
|
|
llvm_unreachable("This is unexpected.");
|
2009-12-09 08:01:00 +08:00
|
|
|
}
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-09 08:01:00 +08:00
|
|
|
// a-ok.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
/// IsPotentiallyPHITranslatable - If this needs PHI translation, return true
|
|
|
|
/// if we have some hope of doing it. This should be used as a filter to
|
|
|
|
/// avoid calling PHITranslateValue in hopeless situations.
|
|
|
|
bool PHITransAddr::IsPotentiallyPHITranslatable() const {
|
|
|
|
// If the input value is not an instruction, or if it is not defined in CurBB,
|
|
|
|
// then we don't need to phi translate it.
|
|
|
|
Instruction *Inst = dyn_cast<Instruction>(Addr);
|
2014-04-15 12:59:12 +08:00
|
|
|
return !Inst || CanPHITrans(Inst);
|
2009-12-08 03:04:49 +08:00
|
|
|
}
|
|
|
|
|
2009-12-08 02:36:53 +08:00
|
|
|
|
2010-11-19 01:06:31 +08:00
|
|
|
static void RemoveInstInputs(Value *V,
|
2009-12-09 07:42:51 +08:00
|
|
|
SmallVectorImpl<Instruction*> &InstInputs) {
|
2009-12-09 08:56:14 +08:00
|
|
|
Instruction *I = dyn_cast<Instruction>(V);
|
2014-04-15 12:59:12 +08:00
|
|
|
if (!I) return;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-09 07:42:51 +08:00
|
|
|
// If the instruction is in the InstInputs list, remove it.
|
|
|
|
SmallVectorImpl<Instruction*>::iterator Entry =
|
|
|
|
std::find(InstInputs.begin(), InstInputs.end(), I);
|
|
|
|
if (Entry != InstInputs.end()) {
|
|
|
|
InstInputs.erase(Entry);
|
|
|
|
return;
|
|
|
|
}
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-09 08:56:14 +08:00
|
|
|
assert(!isa<PHINode>(I) && "Error, removing something that isn't an input");
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-09 07:42:51 +08:00
|
|
|
// Otherwise, it must have instruction inputs itself. Zap them recursively.
|
|
|
|
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
|
2009-12-09 08:56:14 +08:00
|
|
|
if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
|
2009-12-09 07:42:51 +08:00
|
|
|
RemoveInstInputs(Op, InstInputs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-08 02:36:53 +08:00
|
|
|
Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB,
|
2010-02-24 16:48:04 +08:00
|
|
|
BasicBlock *PredBB,
|
|
|
|
const DominatorTree *DT) {
|
2009-12-08 02:36:53 +08:00
|
|
|
// If this is a non-instruction value, it can't require PHI translation.
|
|
|
|
Instruction *Inst = dyn_cast<Instruction>(V);
|
2014-04-15 12:59:12 +08:00
|
|
|
if (!Inst) return V;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-09 08:10:55 +08:00
|
|
|
// Determine whether 'Inst' is an input to our PHI translatable expression.
|
|
|
|
bool isInput = std::count(InstInputs.begin(), InstInputs.end(), Inst);
|
|
|
|
|
|
|
|
// Handle inputs instructions if needed.
|
|
|
|
if (isInput) {
|
|
|
|
if (Inst->getParent() != CurBB) {
|
|
|
|
// If it is an input defined in a different block, then it remains an
|
|
|
|
// input.
|
|
|
|
return Inst;
|
|
|
|
}
|
2009-12-09 08:18:13 +08:00
|
|
|
|
|
|
|
// If 'Inst' is defined in this block and is an input that needs to be phi
|
|
|
|
// translated, we need to incorporate the value into the expression or fail.
|
|
|
|
|
2009-12-09 08:56:14 +08:00
|
|
|
// In either case, the instruction itself isn't an input any longer.
|
|
|
|
InstInputs.erase(std::find(InstInputs.begin(), InstInputs.end(), Inst));
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
// If this is a PHI, go ahead and translate it.
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(Inst))
|
2009-12-09 08:56:14 +08:00
|
|
|
return AddAsInput(PN->getIncomingValueForBlock(PredBB));
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
// If this is a non-phi value, and it is analyzable, we can incorporate it
|
|
|
|
// into the expression by making all instruction operands be inputs.
|
|
|
|
if (!CanPHITrans(Inst))
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
// All instruction operands are now inputs (and of course, they may also be
|
|
|
|
// defined in this block, so they may need to be phi translated themselves.
|
|
|
|
for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
|
|
|
|
if (Instruction *Op = dyn_cast<Instruction>(Inst->getOperand(i)))
|
|
|
|
InstInputs.push_back(Op);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, it must be an intermediate result (either because it started that way
|
|
|
|
// or because we just incorporated it into the expression). See if its
|
|
|
|
// operands need to be phi translated, and if so, reconstruct it.
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2010-11-19 01:05:13 +08:00
|
|
|
if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
|
2014-04-15 12:59:12 +08:00
|
|
|
if (!isSafeToSpeculativelyExecute(Cast)) return nullptr;
|
2010-11-19 01:05:13 +08:00
|
|
|
Value *PHIIn = PHITranslateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT);
|
2014-04-15 12:59:12 +08:00
|
|
|
if (!PHIIn) return nullptr;
|
2010-11-19 01:05:13 +08:00
|
|
|
if (PHIIn == Cast->getOperand(0))
|
|
|
|
return Cast;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
// Find an available version of this cast.
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
// Constants are trivial to find.
|
|
|
|
if (Constant *C = dyn_cast<Constant>(PHIIn))
|
2010-11-19 01:05:13 +08:00
|
|
|
return AddAsInput(ConstantExpr::getCast(Cast->getOpcode(),
|
|
|
|
C, Cast->getType()));
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2010-11-19 01:05:13 +08:00
|
|
|
// Otherwise we have to see if a casted version of the incoming pointer
|
2009-12-08 03:04:49 +08:00
|
|
|
// is available. If so, we can use it, otherwise we have to fail.
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *U : PHIIn->users()) {
|
|
|
|
if (CastInst *CastI = dyn_cast<CastInst>(U))
|
2010-11-19 01:05:13 +08:00
|
|
|
if (CastI->getOpcode() == Cast->getOpcode() &&
|
|
|
|
CastI->getType() == Cast->getType() &&
|
|
|
|
(!DT || DT->dominates(CastI->getParent(), PredBB)))
|
|
|
|
return CastI;
|
2009-12-08 03:04:49 +08:00
|
|
|
}
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2009-12-08 03:04:49 +08:00
|
|
|
}
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
// Handle getelementptr with at least one PHI translatable operand.
|
|
|
|
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
|
|
|
|
SmallVector<Value*, 8> GEPOps;
|
|
|
|
bool AnyChanged = false;
|
|
|
|
for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) {
|
2010-02-24 16:48:04 +08:00
|
|
|
Value *GEPOp = PHITranslateSubExpr(GEP->getOperand(i), CurBB, PredBB, DT);
|
2014-04-15 12:59:12 +08:00
|
|
|
if (!GEPOp) return nullptr;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 03:52:57 +08:00
|
|
|
AnyChanged |= GEPOp != GEP->getOperand(i);
|
2009-12-08 03:04:49 +08:00
|
|
|
GEPOps.push_back(GEPOp);
|
2009-12-08 02:36:53 +08:00
|
|
|
}
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
if (!AnyChanged)
|
|
|
|
return GEP;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
// Simplify the GEP to handle 'gep x, 0' -> x etc.
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
llvm-svn: 217342
2014-09-08 02:57:58 +08:00
|
|
|
if (Value *V = SimplifyGEPInst(GEPOps, DL, TLI, DT, AT)) {
|
2009-12-09 08:56:14 +08:00
|
|
|
for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
|
|
|
|
RemoveInstInputs(GEPOps[i], InstInputs);
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-09 08:56:14 +08:00
|
|
|
return AddAsInput(V);
|
|
|
|
}
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
// Scan to see if we have this GEP available.
|
|
|
|
Value *APHIOp = GEPOps[0];
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *U : APHIOp->users()) {
|
|
|
|
if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(U))
|
2009-12-08 03:04:49 +08:00
|
|
|
if (GEPI->getType() == GEP->getType() &&
|
|
|
|
GEPI->getNumOperands() == GEPOps.size() &&
|
2010-02-24 16:48:04 +08:00
|
|
|
GEPI->getParent()->getParent() == CurBB->getParent() &&
|
|
|
|
(!DT || DT->dominates(GEPI->getParent(), PredBB))) {
|
2009-12-08 03:04:49 +08:00
|
|
|
bool Mismatch = false;
|
|
|
|
for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
|
|
|
|
if (GEPI->getOperand(i) != GEPOps[i]) {
|
|
|
|
Mismatch = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!Mismatch)
|
|
|
|
return GEPI;
|
|
|
|
}
|
2009-12-08 02:36:53 +08:00
|
|
|
}
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2009-12-08 03:04:49 +08:00
|
|
|
}
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
// Handle add with a constant RHS.
|
|
|
|
if (Inst->getOpcode() == Instruction::Add &&
|
|
|
|
isa<ConstantInt>(Inst->getOperand(1))) {
|
|
|
|
// PHI translate the LHS.
|
|
|
|
Constant *RHS = cast<ConstantInt>(Inst->getOperand(1));
|
|
|
|
bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap();
|
|
|
|
bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap();
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2010-02-24 16:48:04 +08:00
|
|
|
Value *LHS = PHITranslateSubExpr(Inst->getOperand(0), CurBB, PredBB, DT);
|
2014-04-15 12:59:12 +08:00
|
|
|
if (!LHS) return nullptr;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
// If the PHI translated LHS is an add of a constant, fold the immediates.
|
|
|
|
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;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-09 08:56:14 +08:00
|
|
|
// If the old 'LHS' was an input, add the new 'LHS' as an input.
|
|
|
|
if (std::count(InstInputs.begin(), InstInputs.end(), BOp)) {
|
|
|
|
RemoveInstInputs(BOp, InstInputs);
|
|
|
|
AddAsInput(LHS);
|
|
|
|
}
|
2009-12-08 03:04:49 +08:00
|
|
|
}
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
// See if the add simplifies away.
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
llvm-svn: 217342
2014-09-08 02:57:58 +08:00
|
|
|
if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, DL, TLI, DT, AT)) {
|
2009-12-09 08:56:14 +08:00
|
|
|
// If we simplified the operands, the LHS is no longer an input, but Res
|
|
|
|
// is.
|
|
|
|
RemoveInstInputs(LHS, InstInputs);
|
|
|
|
return AddAsInput(Res);
|
|
|
|
}
|
2009-12-10 01:27:45 +08:00
|
|
|
|
|
|
|
// If we didn't modify the add, just return it.
|
|
|
|
if (LHS == Inst->getOperand(0) && RHS == Inst->getOperand(1))
|
|
|
|
return Inst;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
// Otherwise, see if we have this add available somewhere.
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *U : LHS->users()) {
|
|
|
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U))
|
2009-12-10 01:18:49 +08:00
|
|
|
if (BO->getOpcode() == Instruction::Add &&
|
|
|
|
BO->getOperand(0) == LHS && BO->getOperand(1) == RHS &&
|
2010-02-24 16:48:04 +08:00
|
|
|
BO->getParent()->getParent() == CurBB->getParent() &&
|
|
|
|
(!DT || DT->dominates(BO->getParent(), PredBB)))
|
2009-12-08 03:04:49 +08:00
|
|
|
return BO;
|
2009-12-08 02:36:53 +08:00
|
|
|
}
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2009-12-08 02:36:53 +08:00
|
|
|
}
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 03:04:49 +08:00
|
|
|
// Otherwise, we failed.
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2009-12-04 10:10:16 +08:00
|
|
|
}
|
|
|
|
|
2009-12-08 02:36:53 +08:00
|
|
|
|
|
|
|
/// PHITranslateValue - PHI translate the current address up the CFG from
|
2010-02-24 16:48:04 +08:00
|
|
|
/// CurBB to Pred, updating our state to reflect any needed changes. If the
|
|
|
|
/// dominator tree DT is non-null, the translated value must dominate
|
|
|
|
/// PredBB. This returns true on failure and sets Addr to null.
|
|
|
|
bool PHITransAddr::PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB,
|
|
|
|
const DominatorTree *DT) {
|
2009-12-09 08:01:00 +08:00
|
|
|
assert(Verify() && "Invalid PHITransAddr!");
|
2010-02-24 16:48:04 +08:00
|
|
|
Addr = PHITranslateSubExpr(Addr, CurBB, PredBB, DT);
|
2009-12-09 08:01:00 +08:00
|
|
|
assert(Verify() && "Invalid PHITransAddr!");
|
2009-12-08 02:36:53 +08:00
|
|
|
|
2010-02-24 16:48:04 +08:00
|
|
|
if (DT) {
|
|
|
|
// Make sure the value is live in the predecessor.
|
|
|
|
if (Instruction *Inst = dyn_cast_or_null<Instruction>(Addr))
|
|
|
|
if (!DT->dominates(Inst->getParent(), PredBB))
|
2014-04-15 12:59:12 +08:00
|
|
|
Addr = nullptr;
|
2010-02-24 16:48:04 +08:00
|
|
|
}
|
2010-02-24 14:55:22 +08:00
|
|
|
|
2014-04-15 12:59:12 +08:00
|
|
|
return Addr == nullptr;
|
2010-02-24 16:48:04 +08:00
|
|
|
}
|
2010-02-24 14:55:22 +08:00
|
|
|
|
2009-12-08 02:36:53 +08:00
|
|
|
/// PHITranslateWithInsertion - PHI translate this value into the specified
|
|
|
|
/// predecessor block, inserting a computation of the value if it is
|
|
|
|
/// unavailable.
|
|
|
|
///
|
|
|
|
/// All newly created instructions are added to the NewInsts list. This
|
|
|
|
/// returns null on failure.
|
|
|
|
///
|
|
|
|
Value *PHITransAddr::
|
|
|
|
PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB,
|
|
|
|
const DominatorTree &DT,
|
|
|
|
SmallVectorImpl<Instruction*> &NewInsts) {
|
|
|
|
unsigned NISize = NewInsts.size();
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 02:36:53 +08:00
|
|
|
// Attempt to PHI translate with insertion.
|
|
|
|
Addr = InsertPHITranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts);
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 02:36:53 +08:00
|
|
|
// If successful, return the new value.
|
|
|
|
if (Addr) return Addr;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 02:36:53 +08:00
|
|
|
// If not, destroy any intermediate instructions inserted.
|
|
|
|
while (NewInsts.size() != NISize)
|
|
|
|
NewInsts.pop_back_val()->eraseFromParent();
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2009-12-08 02:36:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-04 10:10:16 +08:00
|
|
|
/// InsertPHITranslatedPointer - Insert a computation of the PHI translated
|
|
|
|
/// version of 'V' for the edge PredBB->CurBB into the end of the PredBB
|
|
|
|
/// block. All newly created instructions are added to the NewInsts list.
|
|
|
|
/// This returns null on failure.
|
|
|
|
///
|
|
|
|
Value *PHITransAddr::
|
2009-12-08 02:36:53 +08:00
|
|
|
InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB,
|
|
|
|
BasicBlock *PredBB, const DominatorTree &DT,
|
|
|
|
SmallVectorImpl<Instruction*> &NewInsts) {
|
2009-12-04 10:10:16 +08:00
|
|
|
// See if we have a version of this value already available and dominating
|
2009-12-08 02:36:53 +08:00
|
|
|
// PredBB. If so, there is no need to insert a new instance of it.
|
Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
This change, which allows @llvm.assume to be used from within computeKnownBits
(and other associated functions in ValueTracking), adds some (optional)
parameters to computeKnownBits and friends. These functions now (optionally)
take a "context" instruction pointer, an AssumptionTracker pointer, and also a
DomTree pointer, and most of the changes are just to pass this new information
when it is easily available from InstSimplify, InstCombine, etc.
As explained below, the significant conceptual change is that known properties
of a value might depend on the control-flow location of the use (because we
care that the @llvm.assume dominates the use because assumptions have
control-flow dependencies). This means that, when we ask if bits are known in a
value, we might get different answers for different uses.
The significant changes are all in ValueTracking. Two main changes: First, as
with the rest of the code, new parameters need to be passed around. To make
this easier, I grouped them into a structure, and I made internal static
versions of the relevant functions that take this structure as a parameter. The
new code does as you might expect, it looks for @llvm.assume calls that make
use of the value we're trying to learn something about (often indirectly),
attempts to pattern match that expression, and uses the result if successful.
By making use of the AssumptionTracker, the process of finding @llvm.assume
calls is not expensive.
Part of the structure being passed around inside ValueTracking is a set of
already-considered @llvm.assume calls. This is to prevent a query using, for
example, the assume(a == b), to recurse on itself. The context and DT params
are used to find applicable assumptions. An assumption needs to dominate the
context instruction, or come after it deterministically. In this latter case we
only handle the specific case where both the assumption and the context
instruction are in the same block, and we need to exclude assumptions from
being used to simplify their own ephemeral values (those which contribute only
to the assumption) because otherwise the assumption would prove its feeding
comparison trivial and would be removed.
This commit adds the plumbing and the logic for a simple masked-bit propagation
(just enough to write a regression test). Future commits add more patterns
(and, correspondingly, more regression tests).
llvm-svn: 217342
2014-09-08 02:57:58 +08:00
|
|
|
PHITransAddr Tmp(InVal, DL, AT);
|
2010-02-24 16:48:04 +08:00
|
|
|
if (!Tmp.PHITranslateValue(CurBB, PredBB, &DT))
|
|
|
|
return Tmp.getAddr();
|
2009-12-04 10:10:16 +08:00
|
|
|
|
2009-12-08 02:36:53 +08:00
|
|
|
// If we don't have an available version of this value, it must be an
|
|
|
|
// instruction.
|
|
|
|
Instruction *Inst = cast<Instruction>(InVal);
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2010-11-19 01:05:13 +08:00
|
|
|
// Handle cast of PHI translatable value.
|
|
|
|
if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
|
2014-04-15 12:59:12 +08:00
|
|
|
if (!isSafeToSpeculativelyExecute(Cast)) return nullptr;
|
2010-11-19 01:05:13 +08:00
|
|
|
Value *OpVal = InsertPHITranslatedSubExpr(Cast->getOperand(0),
|
2009-12-08 02:36:53 +08:00
|
|
|
CurBB, PredBB, DT, NewInsts);
|
2014-04-15 12:59:12 +08:00
|
|
|
if (!OpVal) return nullptr;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2010-11-19 01:05:13 +08:00
|
|
|
// Otherwise insert a cast at the end of PredBB.
|
|
|
|
CastInst *New = CastInst::Create(Cast->getOpcode(),
|
|
|
|
OpVal, InVal->getType(),
|
|
|
|
InVal->getName()+".phi.trans.insert",
|
|
|
|
PredBB->getTerminator());
|
2009-12-08 02:36:53 +08:00
|
|
|
NewInsts.push_back(New);
|
|
|
|
return New;
|
|
|
|
}
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 02:36:53 +08:00
|
|
|
// Handle getelementptr with at least one PHI operand.
|
|
|
|
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);
|
2014-04-15 12:59:12 +08:00
|
|
|
if (!OpVal) return nullptr;
|
2009-12-08 02:36:53 +08:00
|
|
|
GEPOps.push_back(OpVal);
|
|
|
|
}
|
2010-11-19 01:06:31 +08:00
|
|
|
|
|
|
|
GetElementPtrInst *Result =
|
2011-07-25 23:13:01 +08:00
|
|
|
GetElementPtrInst::Create(GEPOps[0], makeArrayRef(GEPOps).slice(1),
|
2011-07-25 17:48:08 +08:00
|
|
|
InVal->getName()+".phi.trans.insert",
|
|
|
|
PredBB->getTerminator());
|
2009-12-08 02:36:53 +08:00
|
|
|
Result->setIsInBounds(GEP->isInBounds());
|
|
|
|
NewInsts.push_back(Result);
|
|
|
|
return Result;
|
|
|
|
}
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 02:36:53 +08:00
|
|
|
#if 0
|
|
|
|
// FIXME: This code works, but it is unclear that we actually want to insert
|
|
|
|
// a big chain of computation in order to make a value available in a block.
|
|
|
|
// This needs to be evaluated carefully to consider its cost trade offs.
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 02:36:53 +08:00
|
|
|
// Handle add with a constant RHS.
|
|
|
|
if (Inst->getOpcode() == Instruction::Add &&
|
|
|
|
isa<ConstantInt>(Inst->getOperand(1))) {
|
|
|
|
// PHI translate the LHS.
|
|
|
|
Value *OpVal = InsertPHITranslatedSubExpr(Inst->getOperand(0),
|
|
|
|
CurBB, PredBB, DT, NewInsts);
|
|
|
|
if (OpVal == 0) return 0;
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2009-12-08 02:36:53 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
#endif
|
2010-11-19 01:06:31 +08:00
|
|
|
|
2014-04-15 12:59:12 +08:00
|
|
|
return nullptr;
|
2009-12-04 10:10:16 +08:00
|
|
|
}
|