Index: lib/CodeGen/PeepholeOptimizer.cpp =================================================================== --- lib/CodeGen/PeepholeOptimizer.cpp +++ lib/CodeGen/PeepholeOptimizer.cpp @@ -1111,7 +1111,8 @@ getNewSource(MachineRegisterInfo *MRI, const TargetInstrInfo *TII, RegSubRegPair Def, const PeepholeOptimizer::RewriteMapTy &RewriteMap, - bool HandleMultipleSources = true) { + bool HandleMultipleSources = true, + bool Recursing = false) { RegSubRegPair LookupSrc(Def.Reg, Def.SubReg); while (true) { ValueTrackerResult Res = RewriteMap.lookup(LookupSrc); @@ -1131,13 +1132,17 @@ if (!HandleMultipleSources) break; + if (Recursing) + return LookupSrc; + // Multiple sources, recurse into each source to find a new source // for it. Then, rewrite the PHI accordingly to its new edges. SmallVector NewPHISrcs; for (unsigned i = 0; i < NumSrcs; ++i) { RegSubRegPair PHISrc(Res.getSrcReg(i), Res.getSrcSubReg(i)); NewPHISrcs.push_back( - getNewSource(MRI, TII, PHISrc, RewriteMap, HandleMultipleSources)); + getNewSource(MRI, TII, PHISrc, RewriteMap, + HandleMultipleSources, true)); } // Build the new PHI node and return its def register as the new source. Index: test/CodeGen/AArch64/peephole-recursion.mir =================================================================== --- /dev/null +++ test/CodeGen/AArch64/peephole-recursion.mir @@ -0,0 +1,134 @@ +# RUN: llc -o - %s -run-pass=peephole-opt | FileCheck %s +# Make sure this doesn't crash +# CHECK: COPY %r0 + +--- | + target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" + target triple = "thumbv8--linux-gnueabi" + + define i32 @test() { + entry: + ret i32 0 + } + +... +--- +name: test +tracksRegLiveness: true +body: | + bb.0: + successors: %bb.2(0x80000000) + liveins: $r0 + + $0:rgpr = COPY $r0 + t2B %bb.2, 14, $noreg + + bb.1: + successors: %bb.18(0x30000000), %bb.12(0x50000000) + + %7:rgpr = PHI $0, %bb.4, $10, %bb.2 + t2CMPrr $0, $0, 14, $noreg, implicit-def $cpsr + t2Bcc %bb.18, 0, $cpsr + t2B %bb.12, 14, $noreg + + bb.2: + successors: %bb.1(0x30000000) + + $10:rgpr = PHI $0, %bb.0, $291, %bb.58 + t2B %bb.1, 14, $noreg + + bb.4: + successors: %bb.1(0x7c000000) + t2B %bb.1, 14, $noreg + + bb.9: + %54:gpr = PHI $0, %bb.38, $271, %bb.59 + %563:spr = VMOVSR %54, 14, $noreg + tBX_RET 14, $noreg + + bb.12: + successors: %bb.18(0x7c000000) + + bb.18: + successors: %bb.25(0x50000000) + + %91:rgpr = PHI $0, %bb.12, %7, %bb.1 + t2B %bb.25, 14, $noreg + + bb.24: + successors: %bb.25(0x7c000000) + t2B %bb.25, 14, $noreg + + bb.25: + successors: %bb.26(0x50000000) + $127:rgpr = PHI $0, %bb.18, %91, %bb.24 + + bb.26: + successors: %bb.32(0x7c000000) + + t2B %bb.32, 14, $noreg + + bb.31: + successors: %bb.32(0x7c000000) + + bb.32: + successors: %bb.39(0x50000000) + $163:rgpr = PHI $0, %bb.26, $127, %bb.31 + t2B %bb.39, 14, $noreg + + bb.38: + successors: %bb.39(0x7c000000), %bb.9(0x04000000) + + t2CMPrr $0, $0, 14, $noreg, implicit-def $cpsr + t2Bcc %bb.9, 1, $cpsr + + bb.39: + successors: %bb.46(0x50000000) + + $199:rgpr = PHI $0, %bb.32, $163, %bb.38 + t2B %bb.46, 14, $noreg + + bb.45: + successors: %bb.46(0x7c000000) + + bb.46: + successors: %bb.47(0x50000000) + + $235:rgpr = PHI $0, %bb.39, $199, %bb.45 + + bb.47: + successors: %bb.53(0x7c000000) + + t2B %bb.53, 14, $noreg + + bb.52: + successors: %bb.53(0x7c000000) + + bb.53: + successors: %bb.59(0x30000000), %bb.54(0x50000000) + + $271:rgpr = PHI $0, %bb.47, $235, %bb.52 + t2CMPrr $0, $0, 14, $noreg, implicit-def $cpsr + t2Bcc %bb.59, 0, $cpsr + + bb.54: + successors: %bb.57(0x40000000), %bb.58(0x40000000) + + t2CMPrr $0, $0, 14, $noreg, implicit-def $cpsr + t2Bcc %bb.58, 9, $cpsr + + bb.57: + successors: %bb.58(0x80000000) + + bb.58: + successors: %bb.2(0x7c000000) + + $291:gpr = PHI $271, %bb.54, $0, %bb.57 + t2B %bb.2, 14, $noreg + + bb.59: + successors: %bb.9(0x04000000) + + t2B %bb.9, 14, $noreg + +...