Index: lib/Analysis/ValueTracking.cpp =================================================================== --- lib/Analysis/ValueTracking.cpp +++ lib/Analysis/ValueTracking.cpp @@ -3235,6 +3235,10 @@ return false; // The called function could have undefined behavior or // side-effects, even if marked readnone nounwind. } + case Instruction::ExtractElement: + return isa(Inst->getOperand(1)); + case Instruction::InsertElement: + return isa(Inst->getOperand(2)); case Instruction::VAArg: case Instruction::Alloca: case Instruction::Invoke: Index: test/Transforms/SimplifyCFG/speculate-vector-ops.ll =================================================================== --- test/Transforms/SimplifyCFG/speculate-vector-ops.ll +++ test/Transforms/SimplifyCFG/speculate-vector-ops.ll @@ -57,4 +57,40 @@ br label %return } +; CHECK-LABEL: @no_speculate_extractelement_varindex( +; CHECK: br i1 %cmp +; CHECK: extractelement +; CHECK: phi +define i32 @no_speculate_extractelement_varindex(i32 %c, i32 %idx) #0 { +entry: + %cmp = icmp eq i32 %c, 0 + br i1 %cmp, label %a, label %b + +a: + %x = extractelement <4 x i32> undef, i32 %idx + br label %b + +b: + %phi = phi i32 [ 0, %entry], [ %x, %a ] + ret i32 %phi +} + +; CHECK-LABEL: @no_speculate_insertelement_varindex( +; CHECK: br i1 %cmp +; CHECK: insertelement +; CHECK: phi +define <4 x i32> @no_speculate_insertelement_varindex(i32 %c, i32 %idx) #0 { +entry: + %cmp = icmp eq i32 %c, 0 + br i1 %cmp, label %a, label %b + +a: + %x = insertelement <4 x i32> undef, i32 8, i32 %idx + br label %b + +b: + %phi = phi <4 x i32> [ zeroinitializer, %entry], [ %x, %a ] + ret <4 x i32> %phi +} + attributes #0 = { nounwind }