diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2021-07-29 20:15:26 +0000 |
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2021-07-29 20:15:26 +0000 |
| commit | 344a3780b2e33f6ca763666c380202b18aab72a3 (patch) | |
| tree | f0b203ee6eb71d7fdd792373e3c81eb18d6934dd /llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp | |
| parent | b60736ec1405bb0a8dd40989f67ef4c93da068ab (diff) | |
vendor/llvm-project/llvmorg-13-init-16847-g88e66fa60ae5vendor/llvm-project/llvmorg-12.0.1-rc2-0-ge7dac564cd0evendor/llvm-project/llvmorg-12.0.1-0-gfed41342a82f
Diffstat (limited to 'llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp')
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp | 45 |
1 files changed, 28 insertions, 17 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp index c7b5f6f78069..a8474e27383d 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -199,13 +199,21 @@ static Instruction *simplifyAllocaArraySize(InstCombinerImpl &IC, Type *IdxTy = IC.getDataLayout().getIntPtrType(AI.getType()); Value *NullIdx = Constant::getNullValue(IdxTy); Value *Idx[2] = {NullIdx, NullIdx}; - Instruction *GEP = GetElementPtrInst::CreateInBounds( + Instruction *NewI = GetElementPtrInst::CreateInBounds( NewTy, New, Idx, New->getName() + ".sub"); - IC.InsertNewInstBefore(GEP, *It); + IC.InsertNewInstBefore(NewI, *It); + + // Gracefully handle allocas in other address spaces. + if (AI.getType()->getPointerAddressSpace() != + NewI->getType()->getPointerAddressSpace()) { + NewI = + CastInst::CreatePointerBitCastOrAddrSpaceCast(NewI, AI.getType()); + IC.InsertNewInstBefore(NewI, *It); + } // Now make everything use the getelementptr instead of the original // allocation. - return IC.replaceInstUsesWith(AI, GEP); + return IC.replaceInstUsesWith(AI, NewI); } } @@ -264,6 +272,8 @@ bool PointerReplacer::collectUsers(Instruction &I) { return false; } else if (isa<MemTransferInst>(Inst)) { Worklist.insert(Inst); + } else if (Inst->isLifetimeStartOrEnd()) { + continue; } else { LLVM_DEBUG(dbgs() << "Cannot handle pointer user: " << *U << '\n'); return false; @@ -406,7 +416,10 @@ Instruction *InstCombinerImpl::visitAllocaInst(AllocaInst &AI) { Align SourceAlign = getOrEnforceKnownAlignment( TheSrc, AllocaAlign, DL, &AI, &AC, &DT); if (AllocaAlign <= SourceAlign && - isDereferenceableForAllocaSize(TheSrc, &AI, DL)) { + isDereferenceableForAllocaSize(TheSrc, &AI, DL) && + !isa<Instruction>(TheSrc)) { + // FIXME: Can we sink instructions without violating dominance when TheSrc + // is an instruction instead of a constant or argument? LLVM_DEBUG(dbgs() << "Found alloca equal to global: " << AI << '\n'); LLVM_DEBUG(dbgs() << " memcpy = " << *Copy << '\n'); unsigned SrcAddrSpace = TheSrc->getType()->getPointerAddressSpace(); @@ -460,11 +473,11 @@ LoadInst *InstCombinerImpl::combineLoadToNewType(LoadInst &LI, Type *NewTy, Value *Ptr = LI.getPointerOperand(); unsigned AS = LI.getPointerAddressSpace(); + Type *NewPtrTy = NewTy->getPointerTo(AS); Value *NewPtr = nullptr; if (!(match(Ptr, m_BitCast(m_Value(NewPtr))) && - NewPtr->getType()->getPointerElementType() == NewTy && - NewPtr->getType()->getPointerAddressSpace() == AS)) - NewPtr = Builder.CreateBitCast(Ptr, NewTy->getPointerTo(AS)); + NewPtr->getType() == NewPtrTy)) + NewPtr = Builder.CreateBitCast(Ptr, NewPtrTy); LoadInst *NewLoad = Builder.CreateAlignedLoad( NewTy, NewPtr, LI.getAlign(), LI.isVolatile(), LI.getName() + Suffix); @@ -956,10 +969,8 @@ Instruction *InstCombinerImpl::visitLoadInst(LoadInst &LI) { // Do really simple store-to-load forwarding and load CSE, to catch cases // where there are several consecutive memory accesses to the same location, // separated by a few arithmetic operations. - BasicBlock::iterator BBI(LI); bool IsLoadCSE = false; - if (Value *AvailableVal = FindAvailableLoadedValue( - &LI, LI.getParent(), BBI, DefMaxInstsToScan, AA, &IsLoadCSE)) { + if (Value *AvailableVal = FindAvailableLoadedValue(&LI, *AA, &IsLoadCSE)) { if (IsLoadCSE) combineMetadataForCSE(cast<LoadInst>(AvailableVal), &LI, false); @@ -980,10 +991,10 @@ Instruction *InstCombinerImpl::visitLoadInst(LoadInst &LI) { // that this code is not reachable. We do this instead of inserting // an unreachable instruction directly because we cannot modify the // CFG. - StoreInst *SI = new StoreInst(UndefValue::get(LI.getType()), + StoreInst *SI = new StoreInst(PoisonValue::get(LI.getType()), Constant::getNullValue(Op->getType()), &LI); SI->setDebugLoc(LI.getDebugLoc()); - return replaceInstUsesWith(LI, UndefValue::get(LI.getType())); + return replaceInstUsesWith(LI, PoisonValue::get(LI.getType())); } if (Op->hasOneUse()) { @@ -1064,7 +1075,7 @@ static Value *likeBitCastFromVector(InstCombinerImpl &IC, Value *V) { return nullptr; V = IV->getAggregateOperand(); } - if (!isa<UndefValue>(V) ||!U) + if (!match(V, m_Undef()) || !U) return nullptr; auto *UT = cast<VectorType>(U->getType()); @@ -1331,7 +1342,7 @@ static bool removeBitcastsFromLoadStoreOnMinMax(InstCombinerImpl &IC, IC.Builder.SetInsertPoint(USI); combineStoreToNewValue(IC, *USI, NewLI); } - IC.replaceInstUsesWith(*LI, UndefValue::get(LI->getType())); + IC.replaceInstUsesWith(*LI, PoisonValue::get(LI->getType())); IC.eraseInstFromFunction(*LI); return true; } @@ -1395,7 +1406,7 @@ Instruction *InstCombinerImpl::visitStoreInst(StoreInst &SI) { --BBI; // Don't count debug info directives, lest they affect codegen, // and we skip pointer-to-pointer bitcasts, which are NOPs. - if (isa<DbgInfoIntrinsic>(BBI) || + if (BBI->isDebugOrPseudoInst() || (isa<BitCastInst>(BBI) && BBI->getType()->isPointerTy())) { ScanInsts++; continue; @@ -1438,8 +1449,8 @@ Instruction *InstCombinerImpl::visitStoreInst(StoreInst &SI) { // store X, null -> turns into 'unreachable' in SimplifyCFG // store X, GEP(null, Y) -> turns into 'unreachable' in SimplifyCFG if (canSimplifyNullStoreOrGEP(SI)) { - if (!isa<UndefValue>(Val)) - return replaceOperand(SI, 0, UndefValue::get(Val->getType())); + if (!isa<PoisonValue>(Val)) + return replaceOperand(SI, 0, PoisonValue::get(Val->getType())); return nullptr; // Do not modify these! } |
