Index: lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- lib/Transforms/Utils/SimplifyCFG.cpp +++ lib/Transforms/Utils/SimplifyCFG.cpp @@ -508,9 +508,11 @@ // Keep a stack (SmallVector for efficiency) for depth-first traversal SmallVector DFT; + SmallPtrSet Visited; // Initialize - DFT.push_back(V); + if (Visited.insert(V).second) + DFT.push_back(V); while(!DFT.empty()) { V = DFT.pop_back_val(); @@ -518,8 +520,10 @@ if (Instruction *I = dyn_cast(V)) { // If it is a || (or && depending on isEQ), process the operands. if (I->getOpcode() == (isEQ ? Instruction::Or : Instruction::And)) { - DFT.push_back(I->getOperand(1)); - DFT.push_back(I->getOperand(0)); + if (Visited.insert(I->getOperand(1)).second) + DFT.push_back(I->getOperand(1)); + if (Visited.insert(I->getOperand(0)).second) + DFT.push_back(I->getOperand(0)); continue; } Index: test/Transforms/SimplifyCFG/InfLoop.ll =================================================================== --- /dev/null +++ test/Transforms/SimplifyCFG/InfLoop.ll @@ -0,0 +1,171 @@ +; RUN: opt < %s -simplifycfg -disable-output +; END. + +target datalayout = "e-m:o-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32" +target triple = "thumbv7-apple-ios9.0.0" + +%struct.anon = type { %struct.anon.0, i32, i32, %union.T1 } +%struct.anon.0 = type { i32, [256 x i32], [256 x i8] } +%union.T1 = type { %struct.F} +%struct.G_t = type { i32, i32, i32, i32, i32, i8 } +%struct.F = type { i32 } +%struct.S_t = type { %struct.anon.1 } +%struct.anon.1 = type { i64, i64, i64, i64, i64, i64, i64, i64 } +%struct.T1 = type { %union.T1*, i32, %struct.L_t } +%struct.L_t = type { i32*, i32, i32 } +%struct.T2 = type { [256 x i32] } + +@U = internal global %struct.anon zeroinitializer, align 4 +@S = external global %struct.S_t, align 4 +@G = external global %struct.G_t, align 4 +@table = external global [17 x i32], align 4 +@T1 = external global %struct.T1, align 4 +declare i32 @bar() +declare i32 @extend(i32) +declare i8* @update(i32 %t1Ix, i32 %old_sz, i32 %sz) +declare i32 @getix(i8*) + + +define void @main() { +entry: + %0 = load i32, i32* getelementptr inbounds (%struct.anon, %struct.anon* @U, i32 0, i32 2), align 4 + %cmp.i = icmp eq i32 %0, -1 + br i1 %cmp.i, label %if.then, label %if.end + +if.then: ; preds = %entry + br label %if.end + +if.end: ; preds = %entry, %if.then + %1 = load i32, i32* getelementptr inbounds (%struct.anon, %struct.anon* @U, i32 0, i32 2), align 4 + %bf.load = load i32, i32* getelementptr inbounds (%struct.anon, %struct.anon* @U, i32 0, i32 3, i32 0, i32 0), align 4 + %cmp = icmp slt i32 %bf.load, 0 + br i1 %cmp, label %if.end7, label %cond.false + +cond.false: ; preds = %if.end + %bf.clear = shl i32 %bf.load, 1 + %add = and i32 %bf.clear, 30 + %shl = add nuw nsw i32 %add, 2 + br label %if.end7 + +if.end7: ; preds = %if.end, %cond.false + %old_sz.0 = phi i32 [ %shl, %cond.false ], [ 0, %if.end ] + %2 = load i32, i32* getelementptr inbounds (%struct.anon, %struct.anon* @U, i32 0, i32 0, i32 0), align 4 + %cmp.i52 = icmp eq i32 %2, 1 + br i1 %cmp.i52, label %if.then9, label %if.else10 + +if.then9: ; preds = %if.end7 + %3 = load i64, i64* getelementptr inbounds (%struct.S_t, %struct.S_t* @S, i32 0, i32 0, i32 5), align 4 + %inc = add i64 %3, 1 + store i64 %inc, i64* getelementptr inbounds (%struct.S_t, %struct.S_t* @S, i32 0, i32 0, i32 5), align 4 + %4 = load i32, i32* getelementptr inbounds (%struct.anon, %struct.anon* @U, i32 0, i32 0, i32 1, i32 0), align 4 + br label %if.end29 + +if.else10: ; preds = %if.end7 + %cmp11 = icmp ugt i32 %2, 13 + br i1 %cmp11, label %if.then12, label %if.else14 + +if.then12: ; preds = %if.else10 + %call13 = tail call fastcc i8* @update(i32 %1, i32 %old_sz.0, i32 16) + %5 = bitcast i8* %call13 to %struct.T2* + br label %if.end26 + +if.else14: ; preds = %if.else10 + %6 = load i8, i8* getelementptr inbounds (%struct.G_t, %struct.G_t* @G, i32 0, i32 5), align 4 + %tobool = icmp eq i8 %6, 0 + br i1 %tobool, label %lor.rhs, label %if.then18 + +lor.rhs: ; preds = %if.else14 + %tobool.not.i = icmp eq i8 %6, 0 + br i1 %tobool.not.i, label %if.else21, label %if.end.i54 + +if.end.i54: ; preds = %lor.rhs + %add.i = add i32 %2, 1 + %call.i = tail call i32 @extend(i32 %add.i) + %7 = load i32, i32* getelementptr inbounds (%struct.G_t, %struct.G_t* @G, i32 0, i32 3), align 4 + %add1.i = add i32 %7, %call.i + %8 = load i32, i32* getelementptr inbounds (%struct.anon, %struct.anon* @U, i32 0, i32 0, i32 0), align 4 + %shl.i = shl i32 1, %add1.i + %9 = load i32, i32* getelementptr inbounds (%struct.G_t, %struct.G_t* @G, i32 0, i32 4), align 4 + %add9.i = add i32 %9, 1 + br label %for.cond.i + +for.cond.i: ; preds = %if.end6.i, %if.end.i54 + %ix.0.i = phi i32 [ 0, %if.end.i54 ], [ %inc.i55, %if.end6.i ] + %ret.0.off0.i = phi i1 [ false, %if.end.i54 ], [ %.ret.0.off0.i, %if.end6.i ] + %cmp2.i = icmp ult i32 %ix.0.i, %8 + br i1 %cmp2.i, label %for.body.i, label %TmpSimpleNeedExt.exit + +for.body.i: ; preds = %for.cond.i + %arrayidx.i = getelementptr inbounds %struct.anon, %struct.anon* @U, i32 0, i32 0, i32 2, i32 %ix.0.i + %10 = load i8, i8* %arrayidx.i, align 1 + %conv.i = zext i8 %10 to i32 + %cmp3.i = icmp sgt i32 %conv.i, %shl.i + br i1 %cmp3.i, label %if.else21, label %if.end6.i + +if.end6.i: ; preds = %for.body.i + %cmp10.i = icmp ugt i32 %conv.i, %add9.i + %.ret.0.off0.i = or i1 %ret.0.off0.i, %cmp10.i + %inc.i55 = add i32 %ix.0.i, 1 + br label %for.cond.i + +TmpSimpleNeedExt.exit: ; preds = %for.cond.i + br i1 %ret.0.off0.i, label %if.then18, label %if.else21 + +if.then18: ; preds = %if.else14, %TmpSimpleNeedExt.exit + %11 = load i32, i32* getelementptr inbounds (%struct.anon, %struct.anon* @U, i32 0, i32 0, i32 0), align 4 + %add.i56 = add i32 %11, 1 + %arrayidx = getelementptr inbounds [17 x i32], [17 x i32]* @table, i32 0, i32 %add.i56 + %12 = load i32, i32* %arrayidx, align 4 + %call20 = tail call fastcc i8* @update(i32 %1, i32 %old_sz.0, i32 %12) + %13 = bitcast i8* %call20 to %struct.T2* + br label %if.end26 + +if.else21: ; preds = %for.body.i, %lor.rhs, %TmpSimpleNeedExt.exit + %call22 = tail call fastcc i32 @bar() + %arrayidx23 = getelementptr inbounds [17 x i32], [17 x i32]* @table, i32 0, i32 %call22 + %14 = load i32, i32* %arrayidx23, align 4 + %call24 = tail call fastcc i8* @update(i32 %1, i32 %old_sz.0, i32 %14) + %15 = bitcast i8* %call24 to %struct.T2* + br label %if.end26 + +if.end26: ; preds = %if.then18, %if.else21, %if.then12 + %new_.0 = phi i8* [ %call13, %if.then12 ], [ %call20, %if.then18 ], [ %call24, %if.else21 ] + %sz.0 = phi i32 [ 16, %if.then12 ], [ %12, %if.then18 ], [ %14, %if.else21 ] + %call27 = tail call i32 @getix(i8* %new_.0) + %16 = load %union.T1*, %union.T1** getelementptr inbounds (%struct.T1, %struct.T1* @T1, i32 0, i32 0), align 4 + %17 = getelementptr inbounds %union.T1, %union.T1* %16, i32 %1, i32 0, i32 0 + %bf.load.i = load i32, i32* %17, align 4 + %cmp.i51 = icmp slt i32 %bf.load.i, 0 + br i1 %cmp.i51, label %if.then.i, label %if.end.i + +if.then.i: ; preds = %if.end26 + %18 = load i32, i32* getelementptr inbounds (%struct.T1, %struct.T1* @T1, i32 0, i32 2, i32 2), align 4 + %inc.i = add i32 %18, 1 + store i32 %inc.i, i32* getelementptr inbounds (%struct.T1, %struct.T1* @T1, i32 0, i32 2, i32 2), align 4 + br label %if.end.i + +if.end.i: ; preds = %if.then.i, %if.end26 + %and.i = and i32 %call27, 1 + %cmp1.i = icmp eq i32 %and.i, 0 + br i1 %cmp1.i, label %main.exit, label %if.then2.i + +if.then2.i: ; preds = %if.end.i + %.pre.i = load %union.T1*, %union.T1** getelementptr inbounds (%struct.T1, %struct.T1* @T1, i32 0, i32 0), align 4 + br label %main.exit + +main.exit: ; preds = %if.end.i, %if.then2.i + %19 = phi %union.T1* [ %.pre.i, %if.then2.i ], [ %16, %if.end.i ] + %shr.i = lshr i32 %sz.0, 1 + %sub.i = add nuw i32 %shr.i, 15 + %bf.value.i = and i32 %sub.i, 15 + %20 = shl i32 %call27, 3 + %bf.shl.i = and i32 %20, 2147483632 + %bf.set10.i = or i32 %bf.value.i, %bf.shl.i + %ci = getelementptr inbounds %union.T1, %union.T1* %19, i32 %1, i32 0, i32 0 + store i32 %bf.set10.i, i32* %ci, align 1 + br label %if.end29 + +if.end29: ; preds = %main.exit, %if.then9 + store i32 -1, i32* getelementptr inbounds (%struct.anon, %struct.anon* @U, i32 0, i32 2), align 4 + ret void +}