diff --git a/llvm/lib/Target/PowerPC/PPCMIPeephole.cpp b/llvm/lib/Target/PowerPC/PPCMIPeephole.cpp --- a/llvm/lib/Target/PowerPC/PPCMIPeephole.cpp +++ b/llvm/lib/Target/PowerPC/PPCMIPeephole.cpp @@ -919,8 +919,12 @@ LLVM_DEBUG(dbgs() << "With: "); LLVM_DEBUG(MI.dump()); - } else if (isRunOfOnes((unsigned)(FinalMask.getZExtValue()), NewMB, - NewME) || SrcMaskFull) { + } else if ((isRunOfOnes((unsigned)(FinalMask.getZExtValue()), NewMB, + NewME) && NewMB <= NewME)|| SrcMaskFull) { + // Here we only handle MBMI <= MEMI case, so NewMB must be no bigger + // than NewME. Otherwise we get a 64 bit value after folding, but MI + // return a 32 bit value. + // If FoldingReg has only one use and it it not RLWINMo and // RLWINM8o, safe to delete its def SrcMI. Otherwise keep it. if (MRI->hasOneNonDBGUse(FoldingReg) && diff --git a/llvm/test/CodeGen/PowerPC/fold-rlwinm.mir b/llvm/test/CodeGen/PowerPC/fold-rlwinm.mir --- a/llvm/test/CodeGen/PowerPC/fold-rlwinm.mir +++ b/llvm/test/CodeGen/PowerPC/fold-rlwinm.mir @@ -77,6 +77,21 @@ BLR8 implicit $lr8, implicit $rm ... --- +name: testFoldRLWINMResultWrapped +#CHECK : name : testFoldRLWINMResultWrapped +tracksRegLiveness: true +body: | + bb.0.entry: + liveins: $x3 + %0:g8rc = COPY $x3 + %1:gprc = COPY %0.sub_32:g8rc + %2:gprc = RLWINM %1:gprc, 10, 20, 10 + ; CHECK: %2:gprc = RLWINM %1, 10, 20, 10 + %3:gprc = RLWINM %2:gprc, 10, 0, 31 + ; CHECK: %3:gprc = RLWINM %2, 10, 0, 31 + BLR8 implicit $lr8, implicit $rm +... +--- name: testFoldRLWINMMultipleUses #CHECK : name : testFoldRLWINMMultipleUses tracksRegLiveness: true