@@ -253,7 +253,7 @@ bool LoopIdiomVectorize::run(Loop *L) {
253253
254254 if (recognizeMinIdxPattern ())
255255 return true ;
256-
256+
257257 return false ;
258258}
259259
@@ -361,6 +361,14 @@ bool LoopIdiomVectorize::recognizeMinIdxPattern() {
361361 return false ;
362362 }
363363
364+ // In some cases, the pointer used to load comes from a GEP instruction.
365+ // In particular, Flang uses offsetted pointer but for vectorized
366+ // version, we need the base of the GEP.
367+ if (isa<GetElementPtrInst>(BasePtr)) {
368+ LLVM_DEBUG (dbgs () << " Extracting BasePtr from GEP\n " );
369+ BasePtr = cast<GetElementPtrInst>(BasePtr)->getPointerOperand ();
370+ }
371+
364372 // Extract FCmp and validate load types
365373 auto *FCmp = dyn_cast<FCmpInst>(SelectToInspect->getCondition ());
366374 if (!FCmp || !isa<LoadInst>(FCmp->getOperand (0 )) ||
@@ -820,17 +828,13 @@ bool LoopIdiomVectorize::transformMinIdxPattern(
820828 // %minidx.partial.2.if.add) %minidx.partial.2.if.gep = getelementptr inbounds
821829 // nuw float, ptr %p, i64 %FirstIndex
822830 Value *MinIdxPartial2IfAdd =
823- Builder.CreateAdd (MinIdxVectEndLCSSA, ConstantInt::get (I64Ty, 0 ),
831+ Builder.CreateAdd (MinIdxVectEndLCSSA, ConstantInt::get (I64Ty, 1 ),
824832 " minidx.partial.2.if.add.zero" );
825833 Value *MinIdxPartial2IfMask = Builder.CreateCall (
826834 Intrinsic::getOrInsertDeclaration (M, Intrinsic::get_active_lane_mask,
827835 {MaskTy, I64Ty}),
828836 {FirstIndex, MinIdxPartial2IfAdd}, " minidx.partial.2.if.mask" );
829837
830- // Reverse the mask.
831- MinIdxPartial2IfMask = Builder.CreateCall (
832- Intrinsic::getOrInsertDeclaration (M, Intrinsic::vector_reverse, {MaskTy}),
833- {MinIdxPartial2IfMask}, " minidx.partial.2.if.mask.reverse" );
834838
835839 Value *FirstIndexMinus1 =
836840 Builder.CreateSub (FirstIndex, ConstantInt::get (I64Ty, 1 ),
@@ -852,9 +856,16 @@ bool LoopIdiomVectorize::transformMinIdxPattern(
852856 {MinIdxPartial2IfGEP, ConstantInt::get (I32Ty, 1 ),
853857 MinIdxPartial2IfMask, Constant::getNullValue (VecTy)},
854858 " minidx.partial.2.if.load" );
859+ Value *MinIdxPartial2IfSelectVals =
860+ Builder.CreateSelect (MinIdxPartial2IfMask, MinIdxPartial2IfLoad, GMax, " minidx.partial2.if.finalVals" );
861+
862+ // Reverse the mask.
863+ MinIdxPartial2IfMask = Builder.CreateCall (
864+ Intrinsic::getOrInsertDeclaration (M, Intrinsic::vector_reverse, {MaskTy}),
865+ {MinIdxPartial2IfMask}, " minidx.partial.2.if.mask.reverse" );
855866 Value *MinIdxPartial2IfReverse = Builder.CreateCall (
856867 Intrinsic::getOrInsertDeclaration (M, Intrinsic::vector_reverse, {VecTy}),
857- {MinIdxPartial2IfLoad }, " minidx.partial.2.if.reverse" );
868+ {MinIdxPartial2IfSelectVals }, " minidx.partial.2.if.reverse" );
858869 Value *MinIdxPartial2IfReduce = Builder.CreateCall (
859870 Intrinsic::getOrInsertDeclaration (M, Intrinsic::vector_reduce_fminimum,
860871 {VecTy}),
0 commit comments