Index: lib/Transforms/InstCombine/InstCombineCasts.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCasts.cpp +++ lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -1786,6 +1786,102 @@ return ExtractElementInst::Create(NewBC, ExtElt->getIndexOperand()); } +/// This function handles following case +/// +/// A -> B cast +/// PHI +/// B -> A cast +/// +/// All the related PHI nodes can be replaced by new PHI nodes with type A. +/// The uses of \p CI can be changed to the new PHI node corresponding to \p PN. +Instruction *InstCombiner::optimizeBitCastFromPhi(CastInst &CI, PHINode *PN) { + Value *Src = CI.getOperand(0); + Type *SrcTy = Src->getType(); // Type B + Type *DestTy = CI.getType(); // Type A + + SmallVector Worklist; + SmallPtrSet Visited; + + // Find out all the A->B casts and PHI nodes. + Worklist.push_back(PN); + Visited.insert(PN); + while (!Worklist.empty()) { + PHINode *OldPN = Worklist.pop_back_val(); + for (Value *IncValue : OldPN->incoming_values()) { + if (isa(IncValue)) + continue; + + PHINode *PNode = dyn_cast(IncValue); + if (PNode) { + if (Visited.insert(PNode).second) + Worklist.push_back(PNode); + continue; + } + + // Verify it's a A->B cast. + BitCastInst *BCI = dyn_cast(IncValue); + if (BCI) { + Type *TyA = BCI->getOperand(0)->getType(); + Type *TyB = BCI->getType(); + if (TyA != DestTy || TyB != SrcTy) + return nullptr; + continue; + } + + // We can't handle other instructions. + return nullptr; + } + } + + // For each old PHI node, create a corresponding new PHI node with a type A. + SmallDenseMap NewPNodes; + for (auto OldPN : Visited) { + Builder->SetInsertPoint(OldPN); + PHINode *NewPN = Builder->CreatePHI(DestTy, OldPN->getNumOperands()); + NewPNodes[OldPN] = NewPN; + } + + // Fill in the operands of new PHI nodes. + ConstantFolder CFolder; + for (auto OldPN : Visited) { + PHINode *NewPN = NewPNodes[OldPN]; + for (unsigned j = 0, e = OldPN->getNumOperands(); j != e; ++j) { + Value *V = OldPN->getOperand(j); + Value *NewV = nullptr; + if (Constant *C = dyn_cast(V)) { + NewV = CFolder.CreateBitCast(C, DestTy); + } else if (BitCastInst *BCI = dyn_cast(V)) { + NewV = BCI->getOperand(0); + } else if (PHINode *PrevPN = dyn_cast(V)) { + NewV = NewPNodes[PrevPN]; + } + assert(NewV); + NewPN->addIncoming(NewV, OldPN->getIncomingBlock(j)); + } + } + + // If there is a store with type B, change it to type A. + SmallVector ToBeDeleted; + for (User *U : PN->users()) { + StoreInst *SI = dyn_cast(U); + if (SI && SI->getOperand(0) == PN) { + PointerType *OrigPtrTy = + dyn_cast(SI->getOperand(1)->getType()); + Type *DestPtrTy = PointerType::get(DestTy, OrigPtrTy->getAddressSpace()); + Builder->SetInsertPoint(SI); + Builder->CreateAlignedStore( + NewPNodes[PN], + Builder->CreateBitCast(SI->getOperand(1), DestPtrTy), + SI->getAlignment()); + ToBeDeleted.push_back(SI); + } + } + for (unsigned i = 0, e = ToBeDeleted.size(); i != e; ++i) + EraseInstFromFunction(*ToBeDeleted[i]); + + return ReplaceInstUsesWith(CI, NewPNodes[PN]); +} + Instruction *InstCombiner::visitBitCast(BitCastInst &CI) { // If the operands are integer typed then apply the integer transforms, // otherwise just apply the common ones. @@ -1902,6 +1998,12 @@ } } + // Handle the A->B->A cast, and there is an intervening PHI node. + if (PHINode *PN = dyn_cast(Src)) { + if (Instruction *I = optimizeBitCastFromPhi(CI, PN)) + return I; + } + if (Instruction *I = canonicalizeBitCastExtElt(CI, *this, DL)) return I; Index: lib/Transforms/InstCombine/InstCombineInternal.h =================================================================== --- lib/Transforms/InstCombine/InstCombineInternal.h +++ lib/Transforms/InstCombine/InstCombineInternal.h @@ -390,6 +390,7 @@ Value *EmitGEPOffset(User *GEP); Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN); Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef Mask); + Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN); public: /// \brief Inserts an instruction \p New before instruction \p Old Index: test/Transforms/InstCombine/pr25342.ll =================================================================== --- test/Transforms/InstCombine/pr25342.ll +++ test/Transforms/InstCombine/pr25342.ll @@ -0,0 +1,93 @@ +; RUN: opt < %s -instcombine -S | FileCheck %s + +%"struct.std::complex" = type { { float, float } } +@dd = external global %"struct.std::complex", align 4 +@dd2 = external global %"struct.std::complex", align 4 + +define void @_Z3fooi(i32 signext %n) { +entry: + br label %for.cond + +for.cond: + %ldd.sroa.0.0 = phi i32 [ 0, %entry ], [ %5, %for.body ] + %ldd.sroa.6.0 = phi i32 [ 0, %entry ], [ %7, %for.body ] + %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ] + %cmp = icmp slt i32 %i.0, %n + br i1 %cmp, label %for.body, label %for.end + +for.body: + %0 = load float, float* getelementptr inbounds (%"struct.std::complex", %"struct.std::complex"* @dd, i64 0, i32 0, i32 0), align 4 + %1 = load float, float* getelementptr inbounds (%"struct.std::complex", %"struct.std::complex"* @dd, i64 0, i32 0, i32 1), align 4 + %2 = load float, float* getelementptr inbounds (%"struct.std::complex", %"struct.std::complex"* @dd2, i64 0, i32 0, i32 0), align 4 + %3 = load float, float* getelementptr inbounds (%"struct.std::complex", %"struct.std::complex"* @dd2, i64 0, i32 0, i32 1), align 4 + %mul.i = fmul float %0, %2 + %mul4.i = fmul float %1, %3 + %sub.i = fsub float %mul.i, %mul4.i + %mul5.i = fmul float %1, %2 + %mul6.i = fmul float %0, %3 + %add.i4 = fadd float %mul5.i, %mul6.i + %4 = bitcast i32 %ldd.sroa.0.0 to float + %add.i = fadd float %sub.i, %4 + %5 = bitcast float %add.i to i32 + %6 = bitcast i32 %ldd.sroa.6.0 to float + %add4.i = fadd float %add.i4, %6 + %7 = bitcast float %add4.i to i32 + %inc = add nsw i32 %i.0, 1 + br label %for.cond + +for.end: + store i32 %ldd.sroa.0.0, i32* bitcast (%"struct.std::complex"* @dd to i32*), align 4 + store i32 %ldd.sroa.6.0, i32* bitcast (float* getelementptr inbounds (%"struct.std::complex", %"struct.std::complex"* @dd, i64 0, i32 0, i32 1) to i32*), align 4 + ret void + +; CHECK: phi float +; CHECK: store float +; CHECK-NOT: bitcast +} + + +define void @multi_phi(i32 signext %n) { +entry: + br label %for.cond + +for.cond: + %ldd.sroa.0.0 = phi i32 [ 0, %entry ], [ %9, %odd.bb ] + %i.0 = phi i32 [ 0, %entry ], [ %inc, %odd.bb ] + %cmp = icmp slt i32 %i.0, %n + br i1 %cmp, label %for.body, label %for.end + +for.body: + %0 = load float, float* getelementptr inbounds (%"struct.std::complex", %"struct.std::complex"* @dd, i64 0, i32 0, i32 0), align 4 + %1 = load float, float* getelementptr inbounds (%"struct.std::complex", %"struct.std::complex"* @dd, i64 0, i32 0, i32 1), align 4 + %2 = load float, float* getelementptr inbounds (%"struct.std::complex", %"struct.std::complex"* @dd2, i64 0, i32 0, i32 0), align 4 + %3 = load float, float* getelementptr inbounds (%"struct.std::complex", %"struct.std::complex"* @dd2, i64 0, i32 0, i32 1), align 4 + %mul.i = fmul float %0, %2 + %mul4.i = fmul float %1, %3 + %sub.i = fsub float %mul.i, %mul4.i + %4 = bitcast i32 %ldd.sroa.0.0 to float + %add.i = fadd float %sub.i, %4 + %5 = bitcast float %add.i to i32 + %inc = add nsw i32 %i.0, 1 + %bit0 = and i32 %inc, 1 + %even = icmp slt i32 %bit0, 1 + br i1 %even, label %even.bb, label %odd.bb + +even.bb: + %6 = bitcast i32 %5 to float + %7 = fadd float %sub.i, %6 + %8 = bitcast float %7 to i32 + br label %odd.bb + +odd.bb: + %9 = phi i32 [ %5, %for.body ], [ %8, %even.bb ] + br label %for.cond + +for.end: + store i32 %ldd.sroa.0.0, i32* bitcast (%"struct.std::complex"* @dd to i32*), align 4 + ret void + +; CHECK-LABEL: @multi_phi( +; CHECK: phi float +; CHECK: store float +; CHECK-NOT: bitcast +}