From 666aa945a540931451f82cd5e471bc969b1f87bc Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Thu, 14 Jul 2016 06:58:42 +0000 Subject: [PATCH] [InstCombine] Masked loads with undef masks can fold to normal loads We were able to fold masked loads with an all-ones mask to a normal load. However, we couldn't turn a masked load with a mask with mixed ones and undefs into a normal load. llvm-svn: 275380 --- .../Transforms/InstCombine/InstCombineCalls.cpp | 25 ++++++++++++++++------ .../Transforms/InstCombine/masked_intrinsics.ll | 9 ++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index 36c9762..8acff91 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -1038,14 +1038,27 @@ static Value *simplifyMinnumMaxnum(const IntrinsicInst &II) { return nullptr; } -static Value *simplifyMaskedLoad(const IntrinsicInst &II, - InstCombiner::BuilderTy &Builder) { - auto *ConstMask = dyn_cast(II.getArgOperand(2)); +static bool maskIsAllOneOrUndef(Value *Mask) { + auto *ConstMask = dyn_cast(Mask); if (!ConstMask) - return nullptr; + return false; + if (ConstMask->isAllOnesValue() || isa(ConstMask)) + return true; + for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); I != E; + ++I) { + if (auto *MaskElt = ConstMask->getAggregateElement(I)) + if (MaskElt->isAllOnesValue() || isa(MaskElt)) + continue; + return false; + } + return true; +} - // If the mask is all ones, this is a plain vector load of the 1st argument. - if (ConstMask->isAllOnesValue()) { +static Value *simplifyMaskedLoad(const IntrinsicInst &II, + InstCombiner::BuilderTy &Builder) { + // If the mask is all ones or undefs, this is a plain vector load of the 1st + // argument. + if (maskIsAllOneOrUndef(II.getArgOperand(2))) { Value *LoadPtr = II.getArgOperand(0); unsigned Alignment = cast(II.getArgOperand(1))->getZExtValue(); return Builder.CreateAlignedLoad(LoadPtr, Alignment, "unmaskedload"); diff --git a/llvm/test/Transforms/InstCombine/masked_intrinsics.ll b/llvm/test/Transforms/InstCombine/masked_intrinsics.ll index 0b15f7a..ce79ce5 100644 --- a/llvm/test/Transforms/InstCombine/masked_intrinsics.ll +++ b/llvm/test/Transforms/InstCombine/masked_intrinsics.ll @@ -22,6 +22,15 @@ define <2 x double> @load_onemask(<2 x double>* %ptr, <2 x double> %passthru) { ; CHECK-NEXT: ret <2 x double> %unmaskedload } +define <2 x double> @load_undefmask(<2 x double>* %ptr, <2 x double> %passthru) { + %res = call <2 x double> @llvm.masked.load.v2f64.p0v2f64(<2 x double>* %ptr, i32 2, <2 x i1> , <2 x double> %passthru) + ret <2 x double> %res + +; CHECK-LABEL: @load_undefmask( +; CHECK-NEXT: %unmaskedload = load <2 x double>, <2 x double>* %ptr, align 2 +; CHECK-NEXT: ret <2 x double> %unmaskedload +} + define void @store_zeromask(<2 x double>* %ptr, <2 x double> %val) { call void @llvm.masked.store.v2f64.p0v2f64(<2 x double> %val, <2 x double>* %ptr, i32 3, <2 x i1> zeroinitializer) ret void -- 2.7.4