diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2019-08-21 18:13:02 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2019-08-21 18:13:02 +0000 |
commit | 54db30ce18663e6c2991958f3b5d18362e8e93c4 (patch) | |
tree | 4aa6442802570767398cc83ba484e97b1309bdc2 /contrib/llvm/lib/Transforms/Utils/Local.cpp | |
parent | 35284c22e9c8348159b7ce032ea45f2cdeb65298 (diff) | |
parent | e6d1592492a3a379186bfb02bd0f4eda0669c0d5 (diff) |
Merge llvm trunk r366426, resolve conflicts, and update FREEBSD-Xlist.
Notes
Notes:
svn path=/projects/clang900-import/; revision=351344
Diffstat (limited to 'contrib/llvm/lib/Transforms/Utils/Local.cpp')
-rw-r--r-- | contrib/llvm/lib/Transforms/Utils/Local.cpp | 387 |
1 files changed, 229 insertions, 158 deletions
diff --git a/contrib/llvm/lib/Transforms/Utils/Local.cpp b/contrib/llvm/lib/Transforms/Utils/Local.cpp index 499e611acb57..39b6b889f91c 100644 --- a/contrib/llvm/lib/Transforms/Utils/Local.cpp +++ b/contrib/llvm/lib/Transforms/Utils/Local.cpp @@ -1,9 +1,8 @@ //===- Local.cpp - Functions to perform local transformations -------------===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // @@ -27,6 +26,7 @@ #include "llvm/ADT/Statistic.h" #include "llvm/ADT/TinyPtrVector.h" #include "llvm/Analysis/ConstantFolding.h" +#include "llvm/Analysis/DomTreeUpdater.h" #include "llvm/Analysis/EHPersonalities.h" #include "llvm/Analysis/InstructionSimplify.h" #include "llvm/Analysis/LazyValueInfo.h" @@ -49,7 +49,6 @@ #include "llvm/IR/DebugInfoMetadata.h" #include "llvm/IR/DebugLoc.h" #include "llvm/IR/DerivedTypes.h" -#include "llvm/IR/DomTreeUpdater.h" #include "llvm/IR/Dominators.h" #include "llvm/IR/Function.h" #include "llvm/IR/GetElementPtrTypeIterator.h" @@ -92,6 +91,10 @@ using namespace llvm::PatternMatch; STATISTIC(NumRemoved, "Number of unreachable basic blocks removed"); +// Max recursion depth for collectBitParts used when detecting bswap and +// bitreverse idioms +static const unsigned BitPartRecursionMaxDepth = 64; + //===----------------------------------------------------------------------===// // Local constant propagation. // @@ -129,7 +132,7 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions, Builder.CreateBr(Destination); BI->eraseFromParent(); if (DTU) - DTU->deleteEdgeRelaxed(BB, OldDest); + DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, OldDest}}); return true; } @@ -205,7 +208,8 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions, i = SI->removeCase(i); e = SI->case_end(); if (DTU) - DTU->deleteEdgeRelaxed(ParentBB, DefaultDest); + DTU->applyUpdatesPermissive( + {{DominatorTree::Delete, ParentBB, DefaultDest}}); continue; } @@ -253,7 +257,7 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions, if (DeleteDeadConditions) RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI); if (DTU) - DTU->applyUpdates(Updates, /*ForceRemoveDuplicates*/ true); + DTU->applyUpdatesPermissive(Updates); return true; } @@ -331,7 +335,7 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions, } if (DTU) - DTU->applyUpdates(Updates, /*ForceRemoveDuplicates*/ true); + DTU->applyUpdatesPermissive(Updates); return true; } } @@ -416,8 +420,8 @@ bool llvm::wouldInstructionBeTriviallyDead(Instruction *I, if (Constant *C = dyn_cast<Constant>(CI->getArgOperand(0))) return C->isNullValue() || isa<UndefValue>(C); - if (CallSite CS = CallSite(I)) - if (isMathLibCallNoop(CS, TLI)) + if (auto *Call = dyn_cast<CallBase>(I)) + if (isMathLibCallNoop(Call, TLI)) return true; return false; @@ -430,7 +434,7 @@ bool llvm::wouldInstructionBeTriviallyDead(Instruction *I, bool llvm::RecursivelyDeleteTriviallyDeadInstructions( Value *V, const TargetLibraryInfo *TLI, MemorySSAUpdater *MSSAU) { Instruction *I = dyn_cast<Instruction>(V); - if (!I || !I->use_empty() || !isInstructionTriviallyDead(I, TLI)) + if (!I || !isInstructionTriviallyDead(I, TLI)) return false; SmallVector<Instruction*, 16> DeadInsts; @@ -665,7 +669,7 @@ void llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred, if (PhiIt != OldPhiIt) PhiIt = &BB->front(); } if (DTU) - DTU->deleteEdgeRelaxed(Pred, BB); + DTU->applyUpdatesPermissive({{DominatorTree::Delete, Pred, BB}}); } /// MergeBasicBlockIntoOnlyPred - DestBB is a block with one predecessor and its @@ -734,7 +738,7 @@ void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, isa<UnreachableInst>(PredBB->getTerminator()) && "The successor list of PredBB isn't empty before " "applying corresponding DTU updates."); - DTU->applyUpdates(Updates, /*ForceRemoveDuplicates*/ true); + DTU->applyUpdatesPermissive(Updates); DTU->deleteBB(PredBB); // Recalculation of DomTree is needed when updating a forward DomTree and // the Entry BB is replaced. @@ -997,6 +1001,18 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB, } } + // We cannot fold the block if it's a branch to an already present callbr + // successor because that creates duplicate successors. + for (auto I = pred_begin(BB), E = pred_end(BB); I != E; ++I) { + if (auto *CBI = dyn_cast<CallBrInst>((*I)->getTerminator())) { + if (Succ == CBI->getDefaultDest()) + return false; + for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i) + if (Succ == CBI->getIndirectDest(i)) + return false; + } + } + LLVM_DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB); SmallVector<DominatorTree::UpdateType, 32> Updates; @@ -1064,7 +1080,7 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB, "applying corresponding DTU updates."); if (DTU) { - DTU->applyUpdates(Updates, /*ForceRemoveDuplicates*/ true); + DTU->applyUpdatesPermissive(Updates); DTU->deleteBB(BB); } else { BB->eraseFromParent(); // Delete the old basic block. @@ -1272,6 +1288,19 @@ static bool valueCoversEntireFragment(Type *ValTy, DbgVariableIntrinsic *DII) { return false; } +/// Produce a DebugLoc to use for each dbg.declare/inst pair that are promoted +/// to a dbg.value. Because no machine insts can come from debug intrinsics, +/// only the scope and inlinedAt is significant. Zero line numbers are used in +/// case this DebugLoc leaks into any adjacent instructions. +static DebugLoc getDebugValueLoc(DbgVariableIntrinsic *DII, Instruction *Src) { + // Original dbg.declare must have a location. + DebugLoc DeclareLoc = DII->getDebugLoc(); + MDNode *Scope = DeclareLoc.getScope(); + DILocation *InlinedAt = DeclareLoc.getInlinedAt(); + // Produce an unknown location with the correct scope / inlinedAt fields. + return DebugLoc::get(0, 0, Scope, InlinedAt); +} + /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic. void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, @@ -1280,9 +1309,11 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, auto *DIVar = DII->getVariable(); assert(DIVar && "Missing variable"); auto *DIExpr = DII->getExpression(); - Value *DV = SI->getOperand(0); + Value *DV = SI->getValueOperand(); + + DebugLoc NewLoc = getDebugValueLoc(DII, SI); - if (!valueCoversEntireFragment(SI->getValueOperand()->getType(), DII)) { + if (!valueCoversEntireFragment(DV->getType(), DII)) { // FIXME: If storing to a part of the variable described by the dbg.declare, // then we want to insert a dbg.value for the corresponding fragment. LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: " @@ -1292,14 +1323,12 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, // know nothing about the variable's content. DV = UndefValue::get(DV->getType()); if (!LdStHasDebugValue(DIVar, DIExpr, SI)) - Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, DII->getDebugLoc(), - SI); + Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc, SI); return; } if (!LdStHasDebugValue(DIVar, DIExpr, SI)) - Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, DII->getDebugLoc(), - SI); + Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc, SI); } /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value @@ -1322,12 +1351,14 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, return; } + DebugLoc NewLoc = getDebugValueLoc(DII, nullptr); + // We are now tracking the loaded value instead of the address. In the // future if multi-location support is added to the IR, it might be // preferable to keep tracking both the loaded value and the original // address in case the alloca can not be elided. Instruction *DbgValue = Builder.insertDbgValueIntrinsic( - LI, DIVar, DIExpr, DII->getDebugLoc(), (Instruction *)nullptr); + LI, DIVar, DIExpr, NewLoc, (Instruction *)nullptr); DbgValue->insertAfter(LI); } @@ -1354,12 +1385,13 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, BasicBlock *BB = APN->getParent(); auto InsertionPt = BB->getFirstInsertionPt(); + DebugLoc NewLoc = getDebugValueLoc(DII, nullptr); + // The block may be a catchswitch block, which does not have a valid // insertion point. // FIXME: Insert dbg.value markers in the successors when appropriate. if (InsertionPt != BB->end()) - Builder.insertDbgValueIntrinsic(APN, DIVar, DIExpr, DII->getDebugLoc(), - &*InsertionPt); + Builder.insertDbgValueIntrinsic(APN, DIVar, DIExpr, NewLoc, &*InsertionPt); } /// Determine whether this alloca is either a VLA or an array. @@ -1414,10 +1446,11 @@ bool llvm::LowerDbgDeclare(Function &F) { // This is a call by-value or some other instruction that takes a // pointer to the variable. Insert a *value* intrinsic that describes // the variable by dereferencing the alloca. + DebugLoc NewLoc = getDebugValueLoc(DDI, nullptr); auto *DerefExpr = DIExpression::append(DDI->getExpression(), dwarf::DW_OP_deref); - DIB.insertDbgValueIntrinsic(AI, DDI->getVariable(), DerefExpr, - DDI->getDebugLoc(), CI); + DIB.insertDbgValueIntrinsic(AI, DDI->getVariable(), DerefExpr, NewLoc, + CI); } } DDI->eraseFromParent(); @@ -1519,14 +1552,14 @@ void llvm::findDbgUsers(SmallVectorImpl<DbgVariableIntrinsic *> &DbgUsers, bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress, Instruction *InsertBefore, DIBuilder &Builder, - bool DerefBefore, int Offset, bool DerefAfter) { + uint8_t DIExprFlags, int Offset) { auto DbgAddrs = FindDbgAddrUses(Address); for (DbgVariableIntrinsic *DII : DbgAddrs) { DebugLoc Loc = DII->getDebugLoc(); auto *DIVar = DII->getVariable(); auto *DIExpr = DII->getExpression(); assert(DIVar && "Missing variable"); - DIExpr = DIExpression::prepend(DIExpr, DerefBefore, Offset, DerefAfter); + DIExpr = DIExpression::prepend(DIExpr, DIExprFlags, Offset); // Insert llvm.dbg.declare immediately before InsertBefore, and remove old // llvm.dbg.declare. Builder.insertDeclare(NewAddress, DIVar, DIExpr, Loc, InsertBefore); @@ -1538,10 +1571,10 @@ bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress, } bool llvm::replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress, - DIBuilder &Builder, bool DerefBefore, - int Offset, bool DerefAfter) { + DIBuilder &Builder, uint8_t DIExprFlags, + int Offset) { return replaceDbgDeclare(AI, NewAllocaAddress, AI->getNextNode(), Builder, - DerefBefore, Offset, DerefAfter); + DIExprFlags, Offset); } static void replaceOneDbgValueForAlloca(DbgValueInst *DVI, Value *NewAddress, @@ -1594,120 +1627,119 @@ bool llvm::salvageDebugInfo(Instruction &I) { if (DbgUsers.empty()) return false; - auto &M = *I.getModule(); - auto &DL = M.getDataLayout(); + return salvageDebugInfoForDbgValues(I, DbgUsers); +} + +bool llvm::salvageDebugInfoForDbgValues( + Instruction &I, ArrayRef<DbgVariableIntrinsic *> DbgUsers) { auto &Ctx = I.getContext(); auto wrapMD = [&](Value *V) { return wrapValueInMetadata(Ctx, V); }; - auto doSalvage = [&](DbgVariableIntrinsic *DII, SmallVectorImpl<uint64_t> &Ops) { - auto *DIExpr = DII->getExpression(); - if (!Ops.empty()) { - // Do not add DW_OP_stack_value for DbgDeclare and DbgAddr, because they - // are implicitly pointing out the value as a DWARF memory location - // description. - bool WithStackValue = isa<DbgValueInst>(DII); - DIExpr = DIExpression::prependOpcodes(DIExpr, Ops, WithStackValue); - } + for (auto *DII : DbgUsers) { + // Do not add DW_OP_stack_value for DbgDeclare and DbgAddr, because they + // are implicitly pointing out the value as a DWARF memory location + // description. + bool StackValue = isa<DbgValueInst>(DII); + + DIExpression *DIExpr = + salvageDebugInfoImpl(I, DII->getExpression(), StackValue); + + // salvageDebugInfoImpl should fail on examining the first element of + // DbgUsers, or none of them. + if (!DIExpr) + return false; + DII->setOperand(0, wrapMD(I.getOperand(0))); DII->setOperand(2, MetadataAsValue::get(Ctx, DIExpr)); LLVM_DEBUG(dbgs() << "SALVAGE: " << *DII << '\n'); + } + + return true; +} + +DIExpression *llvm::salvageDebugInfoImpl(Instruction &I, + DIExpression *SrcDIExpr, + bool WithStackValue) { + auto &M = *I.getModule(); + auto &DL = M.getDataLayout(); + + // Apply a vector of opcodes to the source DIExpression. + auto doSalvage = [&](SmallVectorImpl<uint64_t> &Ops) -> DIExpression * { + DIExpression *DIExpr = SrcDIExpr; + if (!Ops.empty()) { + DIExpr = DIExpression::prependOpcodes(DIExpr, Ops, WithStackValue); + } + return DIExpr; }; - auto applyOffset = [&](DbgVariableIntrinsic *DII, uint64_t Offset) { + // Apply the given offset to the source DIExpression. + auto applyOffset = [&](uint64_t Offset) -> DIExpression * { SmallVector<uint64_t, 8> Ops; DIExpression::appendOffset(Ops, Offset); - doSalvage(DII, Ops); + return doSalvage(Ops); }; - auto applyOps = [&](DbgVariableIntrinsic *DII, - std::initializer_list<uint64_t> Opcodes) { + // initializer-list helper for applying operators to the source DIExpression. + auto applyOps = + [&](std::initializer_list<uint64_t> Opcodes) -> DIExpression * { SmallVector<uint64_t, 8> Ops(Opcodes); - doSalvage(DII, Ops); + return doSalvage(Ops); }; if (auto *CI = dyn_cast<CastInst>(&I)) { - if (!CI->isNoopCast(DL)) - return false; - - // No-op casts are irrelevant for debug info. - MetadataAsValue *CastSrc = wrapMD(I.getOperand(0)); - for (auto *DII : DbgUsers) { - DII->setOperand(0, CastSrc); - LLVM_DEBUG(dbgs() << "SALVAGE: " << *DII << '\n'); - } - return true; + // No-op casts and zexts are irrelevant for debug info. + if (CI->isNoopCast(DL) || isa<ZExtInst>(&I)) + return SrcDIExpr; + return nullptr; } else if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) { unsigned BitWidth = M.getDataLayout().getIndexSizeInBits(GEP->getPointerAddressSpace()); - // Rewrite a constant GEP into a DIExpression. Since we are performing - // arithmetic to compute the variable's *value* in the DIExpression, we - // need to mark the expression with a DW_OP_stack_value. + // Rewrite a constant GEP into a DIExpression. APInt Offset(BitWidth, 0); - if (GEP->accumulateConstantOffset(M.getDataLayout(), Offset)) - for (auto *DII : DbgUsers) - applyOffset(DII, Offset.getSExtValue()); - return true; + if (GEP->accumulateConstantOffset(M.getDataLayout(), Offset)) { + return applyOffset(Offset.getSExtValue()); + } else { + return nullptr; + } } else if (auto *BI = dyn_cast<BinaryOperator>(&I)) { // Rewrite binary operations with constant integer operands. auto *ConstInt = dyn_cast<ConstantInt>(I.getOperand(1)); if (!ConstInt || ConstInt->getBitWidth() > 64) - return false; + return nullptr; uint64_t Val = ConstInt->getSExtValue(); - for (auto *DII : DbgUsers) { - switch (BI->getOpcode()) { - case Instruction::Add: - applyOffset(DII, Val); - break; - case Instruction::Sub: - applyOffset(DII, -int64_t(Val)); - break; - case Instruction::Mul: - applyOps(DII, {dwarf::DW_OP_constu, Val, dwarf::DW_OP_mul}); - break; - case Instruction::SDiv: - applyOps(DII, {dwarf::DW_OP_constu, Val, dwarf::DW_OP_div}); - break; - case Instruction::SRem: - applyOps(DII, {dwarf::DW_OP_constu, Val, dwarf::DW_OP_mod}); - break; - case Instruction::Or: - applyOps(DII, {dwarf::DW_OP_constu, Val, dwarf::DW_OP_or}); - break; - case Instruction::And: - applyOps(DII, {dwarf::DW_OP_constu, Val, dwarf::DW_OP_and}); - break; - case Instruction::Xor: - applyOps(DII, {dwarf::DW_OP_constu, Val, dwarf::DW_OP_xor}); - break; - case Instruction::Shl: - applyOps(DII, {dwarf::DW_OP_constu, Val, dwarf::DW_OP_shl}); - break; - case Instruction::LShr: - applyOps(DII, {dwarf::DW_OP_constu, Val, dwarf::DW_OP_shr}); - break; - case Instruction::AShr: - applyOps(DII, {dwarf::DW_OP_constu, Val, dwarf::DW_OP_shra}); - break; - default: - // TODO: Salvage constants from each kind of binop we know about. - return false; - } + switch (BI->getOpcode()) { + case Instruction::Add: + return applyOffset(Val); + case Instruction::Sub: + return applyOffset(-int64_t(Val)); + case Instruction::Mul: + return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_mul}); + case Instruction::SDiv: + return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_div}); + case Instruction::SRem: + return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_mod}); + case Instruction::Or: + return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_or}); + case Instruction::And: + return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_and}); + case Instruction::Xor: + return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_xor}); + case Instruction::Shl: + return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_shl}); + case Instruction::LShr: + return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_shr}); + case Instruction::AShr: + return applyOps({dwarf::DW_OP_constu, Val, dwarf::DW_OP_shra}); + default: + // TODO: Salvage constants from each kind of binop we know about. + return nullptr; } - return true; - } else if (isa<LoadInst>(&I)) { - MetadataAsValue *AddrMD = wrapMD(I.getOperand(0)); - for (auto *DII : DbgUsers) { - // Rewrite the load into DW_OP_deref. - auto *DIExpr = DII->getExpression(); - DIExpr = DIExpression::prepend(DIExpr, DIExpression::WithDeref); - DII->setOperand(0, AddrMD); - DII->setOperand(2, MetadataAsValue::get(Ctx, DIExpr)); - LLVM_DEBUG(dbgs() << "SALVAGE: " << *DII << '\n'); - } - return true; + // *Not* to do: we should not attempt to salvage load instructions, + // because the validity and lifetime of a dbg.value containing + // DW_OP_deref becomes difficult to analyze. See PR40628 for examples. } - return false; + return nullptr; } /// A replacement for a dbg.value expression. @@ -1849,21 +1881,10 @@ bool llvm::replaceAllDbgUsesWith(Instruction &From, Value &To, return None; bool Signed = *Signedness == DIBasicType::Signedness::Signed; - - if (!Signed) { - // In the unsigned case, assume that a debugger will initialize the - // high bits to 0 and do a no-op conversion. - return Identity(DII); - } else { - // In the signed case, the high bits are given by sign extension, i.e: - // (To >> (ToBits - 1)) * ((2 ^ FromBits) - 1) - // Calculate the high bits and OR them together with the low bits. - SmallVector<uint64_t, 8> Ops({dwarf::DW_OP_dup, dwarf::DW_OP_constu, - (ToBits - 1), dwarf::DW_OP_shr, - dwarf::DW_OP_lit0, dwarf::DW_OP_not, - dwarf::DW_OP_mul, dwarf::DW_OP_or}); - return DIExpression::appendToStack(DII.getExpression(), Ops); - } + dwarf::TypeKind TK = Signed ? dwarf::DW_ATE_signed : dwarf::DW_ATE_unsigned; + SmallVector<uint64_t, 8> Ops({dwarf::DW_OP_LLVM_convert, ToBits, TK, + dwarf::DW_OP_LLVM_convert, FromBits, TK}); + return DIExpression::appendToStack(DII.getExpression(), Ops); }; return rewriteDebugUsers(From, To, DomPoint, DT, SignOrZeroExt); } @@ -1894,10 +1915,14 @@ unsigned llvm::removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB) { } unsigned llvm::changeToUnreachable(Instruction *I, bool UseLLVMTrap, - bool PreserveLCSSA, DomTreeUpdater *DTU) { + bool PreserveLCSSA, DomTreeUpdater *DTU, + MemorySSAUpdater *MSSAU) { BasicBlock *BB = I->getParent(); std::vector <DominatorTree::UpdateType> Updates; + if (MSSAU) + MSSAU->changeToUnreachable(I); + // Loop over all of the successors, removing BB's entry from any PHI // nodes. if (DTU) @@ -1928,7 +1953,7 @@ unsigned llvm::changeToUnreachable(Instruction *I, bool UseLLVMTrap, ++NumInstrsRemoved; } if (DTU) - DTU->applyUpdates(Updates, /*ForceRemoveDuplicates*/ true); + DTU->applyUpdatesPermissive(Updates); return NumInstrsRemoved; } @@ -1937,8 +1962,8 @@ static void changeToCall(InvokeInst *II, DomTreeUpdater *DTU = nullptr) { SmallVector<Value*, 8> Args(II->arg_begin(), II->arg_end()); SmallVector<OperandBundleDef, 1> OpBundles; II->getOperandBundlesAsDefs(OpBundles); - CallInst *NewCall = CallInst::Create(II->getCalledValue(), Args, OpBundles, - "", II); + CallInst *NewCall = CallInst::Create( + II->getFunctionType(), II->getCalledValue(), Args, OpBundles, "", II); NewCall->takeName(II); NewCall->setCallingConv(II->getCallingConv()); NewCall->setAttributes(II->getAttributes()); @@ -1956,7 +1981,7 @@ static void changeToCall(InvokeInst *II, DomTreeUpdater *DTU = nullptr) { UnwindDestBB->removePredecessor(BB); II->eraseFromParent(); if (DTU) - DTU->deleteEdgeRelaxed(BB, UnwindDestBB); + DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, UnwindDestBB}}); } BasicBlock *llvm::changeToInvokeAndSplitBasicBlock(CallInst *CI, @@ -1981,8 +2006,9 @@ BasicBlock *llvm::changeToInvokeAndSplitBasicBlock(CallInst *CI, // can potentially be avoided with a cleverer API design that we do not have // as of this time. - InvokeInst *II = InvokeInst::Create(CI->getCalledValue(), Split, UnwindEdge, - InvokeArgs, OpBundles, CI->getName(), BB); + InvokeInst *II = + InvokeInst::Create(CI->getFunctionType(), CI->getCalledValue(), Split, + UnwindEdge, InvokeArgs, OpBundles, CI->getName(), BB); II->setDebugLoc(CI->getDebugLoc()); II->setCallingConv(CI->getCallingConv()); II->setAttributes(CI->getAttributes()); @@ -2052,7 +2078,7 @@ static bool markAliveBlocks(Function &F, Changed = true; break; } - if (CI->doesNotReturn()) { + if (CI->doesNotReturn() && !CI->isMustTailCall()) { // If we found a call to a no-return function, insert an unreachable // instruction after it. Make sure there isn't *already* one there // though. @@ -2102,7 +2128,8 @@ static bool markAliveBlocks(Function &F, UnwindDestBB->removePredecessor(II->getParent()); II->eraseFromParent(); if (DTU) - DTU->deleteEdgeRelaxed(BB, UnwindDestBB); + DTU->applyUpdatesPermissive( + {{DominatorTree::Delete, BB, UnwindDestBB}}); } else changeToCall(II, DTU); Changed = true; @@ -2191,7 +2218,7 @@ void llvm::removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU) { TI->replaceAllUsesWith(NewTI); TI->eraseFromParent(); if (DTU) - DTU->deleteEdgeRelaxed(BB, UnwindDest); + DTU->applyUpdatesPermissive({{DominatorTree::Delete, BB, UnwindDest}}); } /// removeUnreachableBlocks - Remove blocks that are not reachable, even @@ -2211,7 +2238,7 @@ bool llvm::removeUnreachableBlocks(Function &F, LazyValueInfo *LVI, assert(Reachable.size() < F.size()); NumRemoved += F.size()-Reachable.size(); - SmallPtrSet<BasicBlock *, 16> DeadBlockSet; + SmallSetVector<BasicBlock *, 8> DeadBlockSet; for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ++I) { auto *BB = &*I; if (Reachable.count(BB)) @@ -2256,7 +2283,7 @@ bool llvm::removeUnreachableBlocks(Function &F, LazyValueInfo *LVI, } if (DTU) { - DTU->applyUpdates(Updates, /*ForceRemoveDuplicates*/ true); + DTU->applyUpdatesPermissive(Updates); bool Deleted = false; for (auto *BB : DeadBlockSet) { if (DTU->isBBPendingDeletion(BB)) @@ -2450,12 +2477,12 @@ unsigned llvm::replaceDominatedUsesWith(Value *From, Value *To, return ::replaceDominatedUsesWith(From, To, BB, ProperlyDominates); } -bool llvm::callsGCLeafFunction(ImmutableCallSite CS, +bool llvm::callsGCLeafFunction(const CallBase *Call, const TargetLibraryInfo &TLI) { // Check if the function is specifically marked as a gc leaf function. - if (CS.hasFnAttr("gc-leaf-function")) + if (Call->hasFnAttr("gc-leaf-function")) return true; - if (const Function *F = CS.getCalledFunction()) { + if (const Function *F = Call->getCalledFunction()) { if (F->hasFnAttribute("gc-leaf-function")) return true; @@ -2469,7 +2496,7 @@ bool llvm::callsGCLeafFunction(ImmutableCallSite CS, // marked as 'gc-leaf-function.' All available Libcalls are // GC-leaf. LibFunc LF; - if (TLI.getLibFunc(CS, LF)) { + if (TLI.getLibFunc(ImmutableCallSite(Call), LF)) { return TLI.has(LF); } @@ -2530,13 +2557,13 @@ void llvm::hoistAllInstructionsInto(BasicBlock *DomBlock, Instruction *InsertPt, BasicBlock *BB) { // Since we are moving the instructions out of its basic block, we do not // retain their original debug locations (DILocations) and debug intrinsic - // instructions (dbg.values). + // instructions. // // Doing so would degrade the debugging experience and adversely affect the // accuracy of profiling information. // // Currently, when hoisting the instructions, we take the following actions: - // - Remove their dbg.values. + // - Remove their debug intrinsic instructions. // - Set their debug locations to the values from the insertion point. // // As per PR39141 (comment #8), the more fundamental reason why the dbg.values @@ -2554,7 +2581,7 @@ void llvm::hoistAllInstructionsInto(BasicBlock *DomBlock, Instruction *InsertPt, I->dropUnknownNonDebugMetadata(); if (I->isUsedByMetadata()) dropDebugUsers(*I); - if (isa<DbgVariableIntrinsic>(I)) { + if (isa<DbgInfoIntrinsic>(I)) { // Remove DbgInfo Intrinsics. II = I->eraseFromParent(); continue; @@ -2613,7 +2640,7 @@ struct BitPart { /// does not invalidate internal references (std::map instead of DenseMap). static const Optional<BitPart> & collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals, - std::map<Value *, Optional<BitPart>> &BPS) { + std::map<Value *, Optional<BitPart>> &BPS, int Depth) { auto I = BPS.find(V); if (I != BPS.end()) return I->second; @@ -2621,13 +2648,19 @@ collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals, auto &Result = BPS[V] = None; auto BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); + // Prevent stack overflow by limiting the recursion depth + if (Depth == BitPartRecursionMaxDepth) { + LLVM_DEBUG(dbgs() << "collectBitParts max recursion depth reached.\n"); + return Result; + } + if (Instruction *I = dyn_cast<Instruction>(V)) { // If this is an or instruction, it may be an inner node of the bswap. if (I->getOpcode() == Instruction::Or) { auto &A = collectBitParts(I->getOperand(0), MatchBSwaps, - MatchBitReversals, BPS); + MatchBitReversals, BPS, Depth + 1); auto &B = collectBitParts(I->getOperand(1), MatchBSwaps, - MatchBitReversals, BPS); + MatchBitReversals, BPS, Depth + 1); if (!A || !B) return Result; @@ -2660,7 +2693,7 @@ collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals, return Result; auto &Res = collectBitParts(I->getOperand(0), MatchBSwaps, - MatchBitReversals, BPS); + MatchBitReversals, BPS, Depth + 1); if (!Res) return Result; Result = Res; @@ -2692,7 +2725,7 @@ collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals, return Result; auto &Res = collectBitParts(I->getOperand(0), MatchBSwaps, - MatchBitReversals, BPS); + MatchBitReversals, BPS, Depth + 1); if (!Res) return Result; Result = Res; @@ -2707,7 +2740,7 @@ collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals, // If this is a zext instruction zero extend the result. if (I->getOpcode() == Instruction::ZExt) { auto &Res = collectBitParts(I->getOperand(0), MatchBSwaps, - MatchBitReversals, BPS); + MatchBitReversals, BPS, Depth + 1); if (!Res) return Result; @@ -2769,7 +2802,7 @@ bool llvm::recognizeBSwapOrBitReverseIdiom( // Try to find all the pieces corresponding to the bswap. std::map<Value *, Optional<BitPart>> BPS; - auto Res = collectBitParts(I, MatchBSwaps, MatchBitReversals, BPS); + auto Res = collectBitParts(I, MatchBSwaps, MatchBitReversals, BPS, 0); if (!Res) return false; auto &BitProvenance = Res->Provenance; @@ -2883,3 +2916,41 @@ bool llvm::canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx) { return true; } } + +using AllocaForValueMapTy = DenseMap<Value *, AllocaInst *>; +AllocaInst *llvm::findAllocaForValue(Value *V, + AllocaForValueMapTy &AllocaForValue) { + if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) + return AI; + // See if we've already calculated (or started to calculate) alloca for a + // given value. + AllocaForValueMapTy::iterator I = AllocaForValue.find(V); + if (I != AllocaForValue.end()) + return I->second; + // Store 0 while we're calculating alloca for value V to avoid + // infinite recursion if the value references itself. + AllocaForValue[V] = nullptr; + AllocaInst *Res = nullptr; + if (CastInst *CI = dyn_cast<CastInst>(V)) + Res = findAllocaForValue(CI->getOperand(0), AllocaForValue); + else if (PHINode *PN = dyn_cast<PHINode>(V)) { + for (Value *IncValue : PN->incoming_values()) { + // Allow self-referencing phi-nodes. + if (IncValue == PN) + continue; + AllocaInst *IncValueAI = findAllocaForValue(IncValue, AllocaForValue); + // AI for incoming values should exist and should all be equal. + if (IncValueAI == nullptr || (Res != nullptr && IncValueAI != Res)) + return nullptr; + Res = IncValueAI; + } + } else if (GetElementPtrInst *EP = dyn_cast<GetElementPtrInst>(V)) { + Res = findAllocaForValue(EP->getPointerOperand(), AllocaForValue); + } else { + LLVM_DEBUG(dbgs() << "Alloca search cancelled on unknown instruction: " + << *V << "\n"); + } + if (Res) + AllocaForValue[V] = Res; + return Res; +} |