15 #ifndef LLVM_ANALYSIS_CFLGRAPH_H
16 #define LLVM_ANALYSIS_CFLGRAPH_H
53 std::vector<NodeInfo> Levels;
57 auto NumLevels = Levels.size();
58 if (NumLevels > Level)
60 Levels.resize(Level + 1);
65 assert(Level < Levels.size());
69 assert(Level < Levels.size());
80 NodeInfo *getNode(
Node N) {
81 auto Itr = ValueImpls.
find(N.Val);
82 if (Itr == ValueImpls.
end() || Itr->second.getNumLevels() <= N.DerefLevel)
84 return &Itr->second.getNodeInfoAtLevel(N.DerefLevel);
92 auto &ValInfo = ValueImpls[N.
Val];
93 auto Changed = ValInfo.addNodeToLevel(N.
DerefLevel);
94 ValInfo.getNodeInfoAtLevel(N.
DerefLevel).Attr |= Attr;
99 auto *Info = getNode(N);
105 auto *FromInfo = getNode(From);
106 assert(FromInfo !=
nullptr);
107 auto *ToInfo = getNode(To);
108 assert(ToInfo !=
nullptr);
111 ToInfo->ReverseEdges.push_back(
Edge{From,
Offset});
115 auto Itr = ValueImpls.
find(N.
Val);
116 if (Itr == ValueImpls.
end() || Itr->second.getNumLevels() <= N.
DerefLevel)
118 return &Itr->second.getNodeInfoAtLevel(N.
DerefLevel);
122 auto *Info = getNode(N);
128 return make_range<const_value_iterator>(ValueImpls.
begin(),
152 class GetEdgesVisitor :
public InstVisitor<GetEdgesVisitor, void> {
164 return CE->
getOpcode() != Instruction::ICmp &&
170 static bool getPossibleTargets(
CallSite CS,
185 if (
auto GVal = dyn_cast<GlobalValue>(Val)) {
189 }
else if (
auto CExpr = dyn_cast<ConstantExpr>(Val)) {
190 if (hasUsefulEdges(CExpr)) {
192 visitConstantExpr(CExpr);
199 assert(From !=
nullptr && To !=
nullptr);
210 void addDerefEdge(Value *From, Value *To,
bool IsRead) {
211 assert(From !=
nullptr && To !=
nullptr);
217 if (!From->getType()->isPointerTy() || !To->getType()->isPointerTy())
222 Graph.addNode(InstantiatedValue{From, 1});
223 Graph.addEdge(InstantiatedValue{From, 1}, InstantiatedValue{To, 0});
225 Graph.addNode(InstantiatedValue{To, 1});
226 Graph.addEdge(InstantiatedValue{From, 0}, InstantiatedValue{To, 1});
230 void addLoadEdge(Value *From, Value *To) { addDerefEdge(From, To,
true); }
231 void addStoreEdge(Value *From, Value *To) { addDerefEdge(From, To,
false); }
234 GetEdgesVisitor(CFLGraphBuilder &Builder,
const DataLayout &DL)
235 : AA(Builder.
Analysis), DL(DL), TLI(Builder.TLI), Graph(Builder.Graph),
236 ReturnValues(Builder.ReturnedValues) {}
238 void visitInstruction(Instruction &) {
242 void visitReturnInst(ReturnInst &Inst) {
243 if (
auto RetVal = Inst.getReturnValue()) {
244 if (RetVal->getType()->isPointerTy()) {
246 ReturnValues.push_back(RetVal);
251 void visitPtrToIntInst(PtrToIntInst &Inst) {
252 auto *
Ptr = Inst.getOperand(0);
256 void visitIntToPtrInst(IntToPtrInst &Inst) {
261 void visitCastInst(CastInst &Inst) {
262 auto *Src = Inst.getOperand(0);
263 addAssignEdge(Src, &Inst);
266 void visitBinaryOperator(BinaryOperator &Inst) {
267 auto *Op1 = Inst.getOperand(0);
268 auto *Op2 = Inst.getOperand(1);
269 addAssignEdge(Op1, &Inst);
270 addAssignEdge(Op2, &Inst);
273 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &Inst) {
274 auto *
Ptr = Inst.getPointerOperand();
275 auto *Val = Inst.getNewValOperand();
276 addStoreEdge(Val,
Ptr);
279 void visitAtomicRMWInst(AtomicRMWInst &Inst) {
280 auto *
Ptr = Inst.getPointerOperand();
281 auto *Val = Inst.getValOperand();
282 addStoreEdge(Val,
Ptr);
285 void visitPHINode(PHINode &Inst) {
286 for (Value *Val : Inst.incoming_values())
287 addAssignEdge(Val, &Inst);
290 void visitGEP(GEPOperator &GEPOp) {
294 if (GEPOp.accumulateConstantOffset(DL, APOffset))
295 Offset = APOffset.getSExtValue();
297 auto *
Op = GEPOp.getPointerOperand();
298 addAssignEdge(
Op, &GEPOp, Offset);
301 void visitGetElementPtrInst(GetElementPtrInst &Inst) {
302 auto *GEPOp = cast<GEPOperator>(&Inst);
306 void visitSelectInst(SelectInst &Inst) {
312 auto *TrueVal = Inst.getTrueValue();
313 auto *FalseVal = Inst.getFalseValue();
314 addAssignEdge(TrueVal, &Inst);
315 addAssignEdge(FalseVal, &Inst);
318 void visitAllocaInst(AllocaInst &Inst) { addNode(&Inst); }
320 void visitLoadInst(LoadInst &Inst) {
321 auto *
Ptr = Inst.getPointerOperand();
323 addLoadEdge(
Ptr, Val);
326 void visitStoreInst(StoreInst &Inst) {
327 auto *
Ptr = Inst.getPointerOperand();
328 auto *Val = Inst.getValueOperand();
329 addStoreEdge(Val,
Ptr);
332 void visitVAArgInst(VAArgInst &Inst) {
341 if (Inst.getType()->isPointerTy())
345 static bool isFunctionExternal(Function *Fn) {
346 return !Fn->hasExactDefinition();
349 bool tryInterproceduralAnalysis(CallSite CS,
350 const SmallVectorImpl<Function *> &Fns) {
357 for (
auto *Fn : Fns) {
358 if (isFunctionExternal(Fn) || Fn->isVarArg())
361 assert(Fn->arg_size() <= CS.arg_size());
362 if (!AA.getAliasSummary(*Fn))
366 for (
auto *Fn : Fns) {
367 auto Summary = AA.getAliasSummary(*Fn);
368 assert(Summary !=
nullptr);
370 auto &RetParamRelations = Summary->RetParamRelations;
371 for (
auto &Relation : RetParamRelations) {
373 if (IRelation.hasValue()) {
374 Graph.addNode(IRelation->From);
375 Graph.addNode(IRelation->To);
376 Graph.addEdge(IRelation->From, IRelation->To);
380 auto &RetParamAttributes = Summary->RetParamAttributes;
381 for (
auto &
Attribute : RetParamAttributes) {
383 if (IAttr.hasValue())
384 Graph.addNode(IAttr->IValue, IAttr->Attr);
391 void visitCallSite(CallSite CS) {
392 auto Inst = CS.getInstruction();
395 for (Value *V : CS.args())
396 if (V->getType()->isPointerTy())
398 if (Inst->getType()->isPointerTy())
413 SmallVector<Function *, 4> Targets;
414 if (getPossibleTargets(CS, Targets))
415 if (tryInterproceduralAnalysis(CS, Targets))
422 if (!CS.onlyReadsMemory())
423 for (Value *V : CS.args()) {
424 if (V->getType()->isPointerTy()) {
434 if (Inst->getType()->isPointerTy()) {
435 auto *Fn = CS.getCalledFunction();
436 if (Fn ==
nullptr || !Fn->returnDoesNotAlias())
447 void visitExtractElementInst(ExtractElementInst &Inst) {
448 auto *
Ptr = Inst.getVectorOperand();
450 addLoadEdge(
Ptr, Val);
453 void visitInsertElementInst(InsertElementInst &Inst) {
454 auto *Vec = Inst.getOperand(0);
455 auto *Val = Inst.getOperand(1);
456 addAssignEdge(Vec, &Inst);
457 addStoreEdge(Val, &Inst);
460 void visitLandingPadInst(LandingPadInst &Inst) {
464 if (Inst.getType()->isPointerTy())
468 void visitInsertValueInst(InsertValueInst &Inst) {
469 auto *Agg = Inst.getOperand(0);
470 auto *Val = Inst.getOperand(1);
471 addAssignEdge(Agg, &Inst);
472 addStoreEdge(Val, &Inst);
475 void visitExtractValueInst(ExtractValueInst &Inst) {
476 auto *
Ptr = Inst.getAggregateOperand();
477 addLoadEdge(
Ptr, &Inst);
480 void visitShuffleVectorInst(ShuffleVectorInst &Inst) {
481 auto *From1 = Inst.getOperand(0);
482 auto *From2 = Inst.getOperand(1);
483 addAssignEdge(From1, &Inst);
484 addAssignEdge(From2, &Inst);
487 void visitConstantExpr(ConstantExpr *CE) {
488 switch (CE->getOpcode()) {
489 case Instruction::GetElementPtr: {
490 auto GEPOp = cast<GEPOperator>(CE);
494 case Instruction::PtrToInt: {
495 auto *
Ptr = CE->getOperand(0);
499 case Instruction::IntToPtr: {
503 case Instruction::BitCast:
504 case Instruction::AddrSpaceCast:
505 case Instruction::Trunc:
506 case Instruction::ZExt:
507 case Instruction::SExt:
508 case Instruction::FPExt:
509 case Instruction::FPTrunc:
510 case Instruction::UIToFP:
511 case Instruction::SIToFP:
512 case Instruction::FPToUI:
513 case Instruction::FPToSI: {
514 auto *Src = CE->getOperand(0);
515 addAssignEdge(Src, CE);
519 auto *TrueVal = CE->getOperand(0);
520 auto *FalseVal = CE->getOperand(1);
521 addAssignEdge(TrueVal, CE);
522 addAssignEdge(FalseVal, CE);
525 case Instruction::InsertElement: {
526 auto *Vec = CE->getOperand(0);
527 auto *Val = CE->getOperand(1);
528 addAssignEdge(Vec, CE);
529 addStoreEdge(Val, CE);
532 case Instruction::ExtractElement: {
533 auto *
Ptr = CE->getOperand(0);
534 addLoadEdge(
Ptr, CE);
537 case Instruction::InsertValue: {
538 auto *Agg = CE->getOperand(0);
539 auto *Val = CE->getOperand(1);
540 addAssignEdge(Agg, CE);
541 addStoreEdge(Val, CE);
544 case Instruction::ExtractValue: {
545 auto *
Ptr = CE->getOperand(0);
546 addLoadEdge(
Ptr, CE);
549 case Instruction::ShuffleVector: {
550 auto *From1 = CE->getOperand(0);
551 auto *From2 = CE->getOperand(1);
552 addAssignEdge(From1, CE);
553 addAssignEdge(From2, CE);
557 case Instruction::Sub:
558 case Instruction::FSub:
559 case Instruction::Mul:
560 case Instruction::FMul:
561 case Instruction::UDiv:
562 case Instruction::SDiv:
563 case Instruction::FDiv:
564 case Instruction::URem:
565 case Instruction::SRem:
566 case Instruction::FRem:
567 case Instruction::And:
568 case Instruction::Or:
569 case Instruction::Xor:
570 case Instruction::Shl:
571 case Instruction::LShr:
572 case Instruction::AShr:
573 case Instruction::ICmp:
574 case Instruction::FCmp: {
575 addAssignEdge(CE->getOperand(0), CE);
576 addAssignEdge(CE->getOperand(1), CE);
589 static bool hasUsefulEdges(Instruction *Inst) {
590 bool IsNonInvokeRetTerminator = isa<TerminatorInst>(Inst) &&
591 !isa<InvokeInst>(Inst) &&
592 !isa<ReturnInst>(Inst);
593 return !isa<CmpInst>(Inst) && !isa<FenceInst>(Inst) &&
594 !IsNonInvokeRetTerminator;
597 void addArgumentToGraph(Argument &Arg) {
598 if (Arg.getType()->isPointerTy()) {
599 Graph.addNode(InstantiatedValue{&Arg, 0},
612 void addInstructionToGraph(GetEdgesVisitor &Visitor, Instruction &Inst) {
613 if (!hasUsefulEdges(&Inst))
621 void buildGraphFrom(Function &Fn) {
622 GetEdgesVisitor Visitor(*
this, Fn.getParent()->getDataLayout());
624 for (
auto &Bb : Fn.getBasicBlockList())
625 for (
auto &Inst : Bb.getInstList())
626 addInstructionToGraph(Visitor, Inst);
628 for (
auto &Arg : Fn.args())
629 addArgumentToGraph(Arg);
634 : Analysis(Analysis), TLI(TLI) {
640 return ReturnedValues;
void push_back(const T &Elt)
A parsed version of the target data layout string in and methods for querying it. ...
Base class for instruction visitors.
AliasAttrs attrFor(Node N) const
This is the result of instantiating InterfaceValue at a particular callsite.
The Program Expression Graph (PEG) of CFL analysis CFLGraph is auxiliary data structure used by CFL-b...
bool addNodeToLevel(unsigned Level)
iterator_range< const_value_iterator > value_mappings() const
unsigned getOpcode() const
Return the opcode at the root of this constant expression.
const CallInst * isFreeCall(const Value *I, const TargetLibraryInfo *TLI)
isFreeCall - Returns non-null if the value is a call to the builtin free()
const SmallVector< Value *, 4 > & getReturnValues() const
A constant value that is initialized with an expression using other constant values.
void addAttr(Node N, AliasAttrs Attr)
iterator find(const_arg_type_t< KeyT > Val)
const NodeInfo & getNodeInfoAtLevel(unsigned Level) const
CFLGraphBuilder(CFLAA &Analysis, const TargetLibraryInfo &TLI, Function &Fn)
bool addNode(Node N, AliasAttrs Attr=AliasAttrs())
AliasAttrs getAttrEscaped()
AttrEscaped represent whether the said pointer comes from a known source but escapes to the unknown w...
Optional< InstantiatedAttr > instantiateExternalAttribute(ExternalAttribute EAttr, CallSite CS)
const NodeInfo * getNode(Node N) const
bool isPointerTy() const
True if this is an instance of PointerType.
AliasAttrs getAttrUnknown()
AttrUnknown represent whether the said pointer comes from a source not known to alias analyses (such ...
A builder class used to create CFLGraph instance from a given function The CFL-AA that uses this buil...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Type * getType() const
All values are typed, get the type of this value.
Provides information about what library functions are available for the current target.
void addEdge(Node From, Node To, int64_t Offset=0)
This file defines various utility types and functions useful to summary-based alias analysis...
A range adaptor for a pair of iterators.
ValueMap::const_iterator const_value_iterator
NodeInfo & getNodeInfoAtLevel(unsigned Level)
const CFLGraph & getCFLGraph() const
llvm_unreachable("Unknown RegBank!")
block Block Frequency Analysis
unsigned getNumLevels() const
static const int64_t UnknownOffset
AliasAttrs getGlobalOrArgAttrFromValue(const Value &Val)
AttrGlobal represent whether the said pointer is a global value.
bool isMallocOrCallocLikeFn(const Value *V, const TargetLibraryInfo *TLI, bool LookThroughBitCast=false)
Tests if a value is a call or invoke to a library function that allocates memory similar to malloc or...
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
std::bitset< NumAliasAttrs > AliasAttrs
These are attributes that an alias analysis can use to mark certain special properties of a given poi...
AliasAttrs getAttrCaller()
AttrCaller represent whether the said pointer comes from a source not known to the current function b...
std::vector< Edge > EdgeList
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
FunTy * getCalledFunction() const
Return the function being called if this is a direct call, otherwise return null (if it's an indirect...
LLVM Value Representation.
Optional< InstantiatedRelation > instantiateExternalRelation(ExternalRelation ERelation, CallSite CS)
static const unsigned MaxSupportedArgsInSummary
The maximum number of arguments we can put into a summary.