17 #include "llvm/Config/llvm-config.h" 27 if (isa<PHINode>(Inst) ||
28 isa<GetElementPtrInst>(Inst))
31 if (isa<CastInst>(Inst) &&
45 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 48 dbgs() <<
"PHITransAddr: null\n";
51 dbgs() <<
"PHITransAddr: " << *Addr <<
"\n";
52 for (
unsigned i = 0, e = InstInputs.size(); i != e; ++i)
53 dbgs() <<
" Input #" << i <<
" is " << *InstInputs[i] <<
"\n";
67 if (Entry != InstInputs.
end()) {
68 InstInputs.
erase(Entry);
75 errs() <<
"Instruction in PHITransAddr is not phi-translatable:\n";
78 "CanPHITrans is wrong.");
93 if (!Addr)
return true;
101 errs() <<
"PHITransAddr contains extra instructions:\n";
102 for (
unsigned i = 0, e = InstInputs.size(); i != e; ++i)
103 errs() <<
" InstInput #" << i <<
" is " << *InstInputs[i] <<
"\n";
130 if (Entry != InstInputs.
end()) {
131 InstInputs.
erase(Entry);
135 assert(!isa<PHINode>(I) &&
"Error, removing something that isn't an input");
166 InstInputs.erase(
find(InstInputs, Inst));
169 if (
PHINode *PN = dyn_cast<PHINode>(Inst))
170 return AddAsInput(PN->getIncomingValueForBlock(PredBB));
181 InstInputs.push_back(
Op);
188 if (
CastInst *Cast = dyn_cast<CastInst>(Inst)) {
190 Value *PHIIn = PHITranslateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT);
191 if (!PHIIn)
return nullptr;
192 if (PHIIn == Cast->getOperand(0))
198 if (
Constant *
C = dyn_cast<Constant>(PHIIn))
205 if (
CastInst *CastI = dyn_cast<CastInst>(U))
206 if (CastI->getOpcode() == Cast->getOpcode() &&
207 CastI->getType() == Cast->getType() &&
208 (!DT || DT->
dominates(CastI->getParent(), PredBB)))
217 bool AnyChanged =
false;
218 for (
unsigned i = 0, e =
GEP->getNumOperands(); i != e; ++i) {
219 Value *GEPOp = PHITranslateSubExpr(
GEP->getOperand(i), CurBB, PredBB, DT);
220 if (!GEPOp)
return nullptr;
222 AnyChanged |= GEPOp !=
GEP->getOperand(i);
231 GEPOps, {DL, TLI, DT, AC})) {
232 for (
unsigned i = 0, e = GEPOps.
size(); i != e; ++i)
235 return AddAsInput(V);
239 Value *APHIOp = GEPOps[0];
242 if (GEPI->getType() ==
GEP->getType() &&
243 GEPI->getNumOperands() == GEPOps.
size() &&
244 GEPI->getParent()->getParent() == CurBB->
getParent() &&
245 (!DT || DT->
dominates(GEPI->getParent(), PredBB))) {
258 bool isNSW = cast<BinaryOperator>(Inst)->hasNoSignedWrap();
259 bool isNUW = cast<BinaryOperator>(Inst)->hasNoUnsignedWrap();
261 Value *LHS = PHITranslateSubExpr(Inst->
getOperand(0), CurBB, PredBB, DT);
262 if (!LHS)
return nullptr;
267 if (
ConstantInt *CI = dyn_cast<ConstantInt>(BOp->getOperand(1))) {
268 LHS = BOp->getOperand(0);
270 isNSW = isNUW =
false;
284 return AddAsInput(Res);
295 BO->getOperand(0) == LHS && BO->getOperand(1) == RHS &&
296 BO->getParent()->getParent() == CurBB->
getParent() &&
297 (!DT || DT->
dominates(BO->getParent(), PredBB)))
316 assert(DT || !MustDominate);
320 PHITranslateSubExpr(Addr, CurBB, PredBB, MustDominate ? DT :
nullptr);
327 if (
Instruction *Inst = dyn_cast_or_null<Instruction>(Addr))
328 if (!DT->dominates(Inst->getParent(), PredBB))
331 return Addr ==
nullptr;
345 unsigned NISize = NewInsts.
size();
348 Addr = InsertPHITranslatedSubExpr(Addr, CurBB, PredBB, DT, NewInsts);
351 if (Addr)
return Addr;
354 while (NewInsts.
size() != NISize)
365 Value *PHITransAddr::
381 if (
CastInst *Cast = dyn_cast<CastInst>(Inst)) {
383 Value *OpVal = InsertPHITranslatedSubExpr(Cast->getOperand(0),
384 CurBB, PredBB, DT, NewInsts);
385 if (!OpVal)
return nullptr;
389 InVal->
getName() +
".phi.trans.insert",
390 PredBB->getTerminator());
400 for (
unsigned i = 0, e =
GEP->getNumOperands(); i != e; ++i) {
401 Value *OpVal = InsertPHITranslatedSubExpr(
GEP->getOperand(i),
402 CurBB, PredBB, DT, NewInsts);
403 if (!OpVal)
return nullptr;
423 isa<ConstantInt>(Inst->getOperand(1))) {
425 Value *OpVal = InsertPHITranslatedSubExpr(Inst->getOperand(0),
426 CurBB, PredBB, DT, NewInsts);
427 if (OpVal == 0)
return 0;
430 InVal->
getName()+
".phi.trans.insert",
431 PredBB->getTerminator());
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
This class represents lattice values for constants.
void push_back(const T &Elt)
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value *> IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
static void RemoveInstInputs(Value *V, SmallVectorImpl< Instruction *> &InstInputs)
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
This is the base class for all instructions that perform data casts.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Value * SimplifyGEPInst(Type *SrcTy, ArrayRef< Value *> Ops, const SimplifyQuery &Q)
Given operands for a GetElementPtrInst, fold the result or return null.
void setIsInBounds(bool b=true)
Set or clear the inbounds flag on this GEP instruction.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Type * getType() const
All values are typed, get the type of this value.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
static BinaryOperator * CreateAdd(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
Value * getOperand(unsigned i) const
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM Basic Block Representation.
PHITransAddr - An address value which tracks and handles phi translation.
This is an important base class in LLVM.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Value * SimplifyAddInst(Value *LHS, Value *RHS, bool isNSW, bool isNUW, const SimplifyQuery &Q)
Given operands for an Add, fold the result or return null.
iterator erase(const_iterator CI)
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
unsigned getNumOperands() const
Value * PHITranslateWithInsertion(BasicBlock *CurBB, BasicBlock *PredBB, const DominatorTree &DT, SmallVectorImpl< Instruction *> &NewInsts)
PHITranslateWithInsertion - PHI translate this value into the specified predecessor block...
This is the shared class of boolean and integer constants.
bool PHITranslateValue(BasicBlock *CurBB, BasicBlock *PredBB, const DominatorTree *DT, bool MustDominate)
PHITranslateValue - PHI translate the current address up the CFG from CurBB to Pred, updating our state to reflect any needed changes.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
LLVM_NODISCARD T pop_back_val()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
typename SuperClass::iterator iterator
iterator_range< user_iterator > users()
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
static bool CanPHITrans(Instruction *Inst)
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
StringRef getName() const
Return a constant reference to the value's name.
const Function * getParent() const
Return the enclosing method, or null if none.
bool Verify() const
Verify - Check internal consistency of this data structure.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
bool IsPotentiallyPHITranslatable() const
IsPotentiallyPHITranslatable - If this needs PHI translation, return true if we have some hope of doi...
static bool VerifySubExpr(Value *Expr, SmallVectorImpl< Instruction *> &InstInputs)
void setHasNoUnsignedWrap(bool b=true)
Set or clear the nuw flag on this instruction, which must be an operator which supports this flag...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
static bool isInput(const StringSet<> &Prefixes, StringRef Arg)
LLVM Value Representation.
const BasicBlock * getParent() const
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.