comparison lib/Analysis/GlobalsModRef.cpp @ 100:7d135dc70f03

LLVM 3.9
author Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
date Tue, 26 Jan 2016 22:53:40 +0900
parents afa8332a0e37
children 1172e4bd9c6f
comparison
equal deleted inserted replaced
96:6418606d0ead 100:7d135dc70f03
308 FunctionInfos[Writer].addModRefInfoForGlobal(GV, MRI_Mod); 308 FunctionInfos[Writer].addModRefInfoForGlobal(GV, MRI_Mod);
309 } 309 }
310 ++NumNonAddrTakenGlobalVars; 310 ++NumNonAddrTakenGlobalVars;
311 311
312 // If this global holds a pointer type, see if it is an indirect global. 312 // If this global holds a pointer type, see if it is an indirect global.
313 if (GV.getType()->getElementType()->isPointerTy() && 313 if (GV.getValueType()->isPointerTy() &&
314 AnalyzeIndirectGlobalMemory(&GV)) 314 AnalyzeIndirectGlobalMemory(&GV))
315 ++NumIndirectGlobalVars; 315 ++NumIndirectGlobalVars;
316 } 316 }
317 Readers.clear(); 317 Readers.clear();
318 Writers.clear(); 318 Writers.clear();
351 if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest)) 351 if (AnalyzeUsesOfPointer(I, Readers, Writers, OkayStoreDest))
352 return true; 352 return true;
353 } else if (auto CS = CallSite(I)) { 353 } else if (auto CS = CallSite(I)) {
354 // Make sure that this is just the function being called, not that it is 354 // Make sure that this is just the function being called, not that it is
355 // passing into the function. 355 // passing into the function.
356 if (!CS.isCallee(&U)) { 356 if (CS.isDataOperand(&U)) {
357 // Detect calls to free. 357 // Detect calls to free.
358 if (isFreeCall(I, &TLI)) { 358 if (CS.isArgOperand(&U) && isFreeCall(I, &TLI)) {
359 if (Writers) 359 if (Writers)
360 Writers->insert(CS->getParent()->getParent()); 360 Writers->insert(CS->getParent()->getParent());
361 } else if (CS.doesNotCapture(CS.getArgumentNo(&U))) {
362 Function *ParentF = CS->getParent()->getParent();
363 // A nocapture argument may be read from or written to, but does not
364 // escape unless the call can somehow recurse.
365 //
366 // nocapture "indicates that the callee does not make any copies of
367 // the pointer that outlive itself". Therefore if we directly or
368 // indirectly recurse, we must treat the pointer as escaping.
369 if (FunctionToSCCMap[ParentF] ==
370 FunctionToSCCMap[CS.getCalledFunction()])
371 return true;
372 if (Readers)
373 Readers->insert(ParentF);
374 if (Writers)
375 Writers->insert(ParentF);
376 } else { 361 } else {
377 return true; // Argument of an unknown call. 362 return true; // Argument of an unknown call.
378 } 363 }
379 } 364 }
380 } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) { 365 } else if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {
393 /// heap memory: that is, all initializers of the globals are allocations, and 378 /// heap memory: that is, all initializers of the globals are allocations, and
394 /// those allocations have no use other than initialization of the global. 379 /// those allocations have no use other than initialization of the global.
395 /// Further, all loads out of GV must directly use the memory, not store the 380 /// Further, all loads out of GV must directly use the memory, not store the
396 /// pointer somewhere. If this is true, we consider the memory pointed to by 381 /// pointer somewhere. If this is true, we consider the memory pointed to by
397 /// GV to be owned by GV and can disambiguate other pointers from it. 382 /// GV to be owned by GV and can disambiguate other pointers from it.
398 bool GlobalsAAResult::AnalyzeIndirectGlobalMemory(GlobalValue *GV) { 383 bool GlobalsAAResult::AnalyzeIndirectGlobalMemory(GlobalVariable *GV) {
399 // Keep track of values related to the allocation of the memory, f.e. the 384 // Keep track of values related to the allocation of the memory, f.e. the
400 // value produced by the malloc call and any casts. 385 // value produced by the malloc call and any casts.
401 std::vector<Value *> AllocRelatedValues; 386 std::vector<Value *> AllocRelatedValues;
402 387
388 // If the initializer is a valid pointer, bail.
389 if (Constant *C = GV->getInitializer())
390 if (!C->isNullValue())
391 return false;
392
403 // Walk the user list of the global. If we find anything other than a direct 393 // Walk the user list of the global. If we find anything other than a direct
404 // load or store, bail out. 394 // load or store, bail out.
405 for (User *U : GV->users()) { 395 for (User *U : GV->users()) {
406 if (LoadInst *LI = dyn_cast<LoadInst>(U)) { 396 if (LoadInst *LI = dyn_cast<LoadInst>(U)) {
407 // The pointer loaded from the global can only be used in simple ways: 397 // The pointer loaded from the global can only be used in simple ways:
478 // visit all callees before callers (leaf-first). 468 // visit all callees before callers (leaf-first).
479 for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) { 469 for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
480 const std::vector<CallGraphNode *> &SCC = *I; 470 const std::vector<CallGraphNode *> &SCC = *I;
481 assert(!SCC.empty() && "SCC with no functions?"); 471 assert(!SCC.empty() && "SCC with no functions?");
482 472
483 if (!SCC[0]->getFunction()) { 473 if (!SCC[0]->getFunction() || SCC[0]->getFunction()->mayBeOverridden()) {
484 // Calls externally - can't say anything useful. Remove any existing 474 // Calls externally or is weak - can't say anything useful. Remove any existing
485 // function records (may have been created when scanning globals). 475 // function records (may have been created when scanning globals).
486 for (auto *Node : SCC) 476 for (auto *Node : SCC)
487 FunctionInfos.erase(Node->getFunction()); 477 FunctionInfos.erase(Node->getFunction());
488 continue; 478 continue;
489 } 479 }
585 if (FI.getModRefInfo() == MRI_NoModRef) 575 if (FI.getModRefInfo() == MRI_NoModRef)
586 ++NumNoMemFunctions; 576 ++NumNoMemFunctions;
587 577
588 // Finally, now that we know the full effect on this SCC, clone the 578 // Finally, now that we know the full effect on this SCC, clone the
589 // information to each function in the SCC. 579 // information to each function in the SCC.
580 // FI is a reference into FunctionInfos, so copy it now so that it doesn't
581 // get invalidated if DenseMap decides to re-hash.
582 FunctionInfo CachedFI = FI;
590 for (unsigned i = 1, e = SCC.size(); i != e; ++i) 583 for (unsigned i = 1, e = SCC.size(); i != e; ++i)
591 FunctionInfos[SCC[i]->getFunction()] = FI; 584 FunctionInfos[SCC[i]->getFunction()] = CachedFI;
592 } 585 }
586 }
587
588 // GV is a non-escaping global. V is a pointer address that has been loaded from.
589 // If we can prove that V must escape, we can conclude that a load from V cannot
590 // alias GV.
591 static bool isNonEscapingGlobalNoAliasWithLoad(const GlobalValue *GV,
592 const Value *V,
593 int &Depth,
594 const DataLayout &DL) {
595 SmallPtrSet<const Value *, 8> Visited;
596 SmallVector<const Value *, 8> Inputs;
597 Visited.insert(V);
598 Inputs.push_back(V);
599 do {
600 const Value *Input = Inputs.pop_back_val();
601
602 if (isa<GlobalValue>(Input) || isa<Argument>(Input) || isa<CallInst>(Input) ||
603 isa<InvokeInst>(Input))
604 // Arguments to functions or returns from functions are inherently
605 // escaping, so we can immediately classify those as not aliasing any
606 // non-addr-taken globals.
607 //
608 // (Transitive) loads from a global are also safe - if this aliased
609 // another global, its address would escape, so no alias.
610 continue;
611
612 // Recurse through a limited number of selects, loads and PHIs. This is an
613 // arbitrary depth of 4, lower numbers could be used to fix compile time
614 // issues if needed, but this is generally expected to be only be important
615 // for small depths.
616 if (++Depth > 4)
617 return false;
618
619 if (auto *LI = dyn_cast<LoadInst>(Input)) {
620 Inputs.push_back(GetUnderlyingObject(LI->getPointerOperand(), DL));
621 continue;
622 }
623 if (auto *SI = dyn_cast<SelectInst>(Input)) {
624 const Value *LHS = GetUnderlyingObject(SI->getTrueValue(), DL);
625 const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), DL);
626 if (Visited.insert(LHS).second)
627 Inputs.push_back(LHS);
628 if (Visited.insert(RHS).second)
629 Inputs.push_back(RHS);
630 continue;
631 }
632 if (auto *PN = dyn_cast<PHINode>(Input)) {
633 for (const Value *Op : PN->incoming_values()) {
634 Op = GetUnderlyingObject(Op, DL);
635 if (Visited.insert(Op).second)
636 Inputs.push_back(Op);
637 }
638 continue;
639 }
640
641 return false;
642 } while (!Inputs.empty());
643
644 // All inputs were known to be no-alias.
645 return true;
593 } 646 }
594 647
595 // There are particular cases where we can conclude no-alias between 648 // There are particular cases where we can conclude no-alias between
596 // a non-addr-taken global and some other underlying object. Specifically, 649 // a non-addr-taken global and some other underlying object. Specifically,
597 // a non-addr-taken global is known to not be escaped from any function. It is 650 // a non-addr-taken global is known to not be escaped from any function. It is
664 // Arguments to functions or returns from functions are inherently 717 // Arguments to functions or returns from functions are inherently
665 // escaping, so we can immediately classify those as not aliasing any 718 // escaping, so we can immediately classify those as not aliasing any
666 // non-addr-taken globals. 719 // non-addr-taken globals.
667 continue; 720 continue;
668 } 721 }
669 if (auto *LI = dyn_cast<LoadInst>(Input)) { 722
670 // A pointer loaded from a global would have been captured, and we know 723 // Recurse through a limited number of selects, loads and PHIs. This is an
671 // that the global is non-escaping, so no alias.
672 if (isa<GlobalValue>(GetUnderlyingObject(LI->getPointerOperand(), DL)))
673 continue;
674
675 // Otherwise, a load could come from anywhere, so bail.
676 return false;
677 }
678
679 // Recurse through a limited number of selects and PHIs. This is an
680 // arbitrary depth of 4, lower numbers could be used to fix compile time 724 // arbitrary depth of 4, lower numbers could be used to fix compile time
681 // issues if needed, but this is generally expected to be only be important 725 // issues if needed, but this is generally expected to be only be important
682 // for small depths. 726 // for small depths.
683 if (++Depth > 4) 727 if (++Depth > 4)
684 return false; 728 return false;
729
730 if (auto *LI = dyn_cast<LoadInst>(Input)) {
731 // A pointer loaded from a global would have been captured, and we know
732 // that the global is non-escaping, so no alias.
733 const Value *Ptr = GetUnderlyingObject(LI->getPointerOperand(), DL);
734 if (isNonEscapingGlobalNoAliasWithLoad(GV, Ptr, Depth, DL))
735 // The load does not alias with GV.
736 continue;
737 // Otherwise, a load could come from anywhere, so bail.
738 return false;
739 }
685 if (auto *SI = dyn_cast<SelectInst>(Input)) { 740 if (auto *SI = dyn_cast<SelectInst>(Input)) {
686 const Value *LHS = GetUnderlyingObject(SI->getTrueValue(), DL); 741 const Value *LHS = GetUnderlyingObject(SI->getTrueValue(), DL);
687 const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), DL); 742 const Value *RHS = GetUnderlyingObject(SI->getFalseValue(), DL);
688 if (Visited.insert(LHS).second) 743 if (Visited.insert(LHS).second)
689 Inputs.push_back(LHS); 744 Inputs.push_back(LHS);
806 for (auto &A : CS.args()) { 861 for (auto &A : CS.args()) {
807 SmallVector<Value*, 4> Objects; 862 SmallVector<Value*, 4> Objects;
808 GetUnderlyingObjects(A, Objects, DL); 863 GetUnderlyingObjects(A, Objects, DL);
809 864
810 // All objects must be identified. 865 // All objects must be identified.
811 if (!std::all_of(Objects.begin(), Objects.end(), [&GV](const Value *V) { 866 if (!std::all_of(Objects.begin(), Objects.end(), isIdentifiedObject) &&
812 return isIdentifiedObject(V); 867 // Try ::alias to see if all objects are known not to alias GV.
813 })) 868 !std::all_of(Objects.begin(), Objects.end(), [&](Value *V) {
869 return this->alias(MemoryLocation(V), MemoryLocation(GV)) == NoAlias;
870 }))
814 return ConservativeResult; 871 return ConservativeResult;
815 872
816 if (std::find(Objects.begin(), Objects.end(), GV) != Objects.end()) 873 if (std::find(Objects.begin(), Objects.end(), GV) != Objects.end())
817 return ConservativeResult; 874 return ConservativeResult;
818 } 875 }