Skip to content

Commit e0edb66

Browse files
committedDec 13, 2017
Reintroduce r320049, r320014 and r319894.
OpenGL issues should be fixed by now. llvm-svn: 320568
1 parent e8d4e88 commit e0edb66

File tree

9 files changed

+91
-5
lines changed

9 files changed

+91
-5
lines changed
 

‎llvm/include/llvm/Analysis/ConstantFolding.h

+7
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ Constant *ConstantFoldInsertValueInstruction(Constant *Agg, Constant *Val,
102102
Constant *ConstantFoldExtractValueInstruction(Constant *Agg,
103103
ArrayRef<unsigned> Idxs);
104104

105+
/// \brief Attempt to constant fold an insertelement instruction with the
106+
/// specified operands and indices. The constant result is returned if
107+
/// successful; if not, null is returned.
108+
Constant *ConstantFoldInsertElementInstruction(Constant *Val,
109+
Constant *Elt,
110+
Constant *Idx);
111+
105112
/// \brief Attempt to constant fold an extractelement instruction with the
106113
/// specified operands and indices. The constant result is returned if
107114
/// successful; if not, null is returned.

‎llvm/include/llvm/Analysis/InstructionSimplify.h

+4
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops,
161161
Value *SimplifyInsertValueInst(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
162162
const SimplifyQuery &Q);
163163

164+
/// Given operands for an InsertElement, fold the result or return null.
165+
Value *SimplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx,
166+
const SimplifyQuery &Q);
167+
164168
/// Given operands for an ExtractValueInst, fold the result or return null.
165169
Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
166170
const SimplifyQuery &Q);

‎llvm/lib/Analysis/InstructionSimplify.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -3827,6 +3827,28 @@ Value *llvm::SimplifyInsertValueInst(Value *Agg, Value *Val,
38273827
return ::SimplifyInsertValueInst(Agg, Val, Idxs, Q, RecursionLimit);
38283828
}
38293829

3830+
Value *llvm::SimplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx,
3831+
const SimplifyQuery &Q) {
3832+
// Try to constant fold.
3833+
auto *VecC = dyn_cast<Constant>(Vec);
3834+
auto *ValC = dyn_cast<Constant>(Val);
3835+
auto *IdxC = dyn_cast<Constant>(Idx);
3836+
if (VecC && ValC && IdxC)
3837+
return ConstantFoldInsertElementInstruction(VecC, ValC, IdxC);
3838+
3839+
// Fold into undef if index is out of bounds.
3840+
if (auto *CI = dyn_cast<ConstantInt>(Idx)) {
3841+
uint64_t NumElements = cast<VectorType>(Vec->getType())->getNumElements();
3842+
3843+
if (CI->uge(NumElements))
3844+
return UndefValue::get(Vec->getType());
3845+
}
3846+
3847+
// TODO: We should also fold if index is iteslf an undef.
3848+
3849+
return nullptr;
3850+
}
3851+
38303852
/// Given operands for an ExtractValueInst, see if we can fold the result.
38313853
/// If not, this returns null.
38323854
static Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
@@ -4700,6 +4722,12 @@ Value *llvm::SimplifyInstruction(Instruction *I, const SimplifyQuery &SQ,
47004722
IV->getIndices(), Q);
47014723
break;
47024724
}
4725+
case Instruction::InsertElement: {
4726+
auto *IE = cast<InsertElementInst>(I);
4727+
Result = SimplifyInsertElementInst(IE->getOperand(0), IE->getOperand(1),
4728+
IE->getOperand(2), Q);
4729+
break;
4730+
}
47034731
case Instruction::ExtractValue: {
47044732
auto *EVI = cast<ExtractValueInst>(I);
47054733
Result = SimplifyExtractValueInst(EVI->getAggregateOperand(),

‎llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,10 @@ Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) {
781781
Value *ScalarOp = IE.getOperand(1);
782782
Value *IdxOp = IE.getOperand(2);
783783

784+
if (auto *V = SimplifyInsertElementInst(
785+
VecOp, ScalarOp, IdxOp, SQ.getWithInstruction(&IE)))
786+
return replaceInstUsesWith(IE, V);
787+
784788
// Inserting an undef or into an undefined place, remove this.
785789
if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
786790
replaceInstUsesWith(IE, VecOp);

‎llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll

+8
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ define i128 @test_non64bit(i128 %a) {
3131
}
3232

3333
declare void @llvm.assume(i1)
34+
35+
define <4 x double> @inselt_bad_index(<4 x double> %a) {
36+
; CHECK-LABEL: @inselt_bad_index(
37+
; CHECK-NEXT: ret <4 x double> undef
38+
;
39+
%I = insertelement <4 x double> %a, double 0.0, i64 4294967296
40+
ret <4 x double> %I
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
; RUN: opt -S -instcombine < %s | FileCheck %s
2+
%S = type { i16, i32 }
3+
4+
define <2 x i16> @test1() {
5+
entry:
6+
%b = insertelement <2 x i16> <i16 undef, i16 0>, i16 extractvalue (%S select (i1 icmp eq (i16 extractelement (<2 x i16> bitcast (<1 x i32> <i32 1> to <2 x i16>), i32 0), i16 0), %S zeroinitializer, %S { i16 0, i32 1 }), 0), i32 0
7+
ret <2 x i16> %b
8+
}
9+
10+
; CHECK-LABEL: @test1(
11+
; CHECK: ret <2 x i16> zeroinitializer

‎llvm/test/Transforms/InstCombine/vector_insertelt_shuffle.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ define <4 x float> @bazz(<4 x float> %x, i32 %a) {
5454
ret <4 x float> %ins6
5555
}
5656

57+
; Out of bounds index folds to undef
5758
define <4 x float> @bazzz(<4 x float> %x) {
5859
; CHECK-LABEL: @bazzz(
59-
; CHECK-NEXT: [[INS2:%.*]] = insertelement <4 x float> %x, float 2.000000e+00, i32 2
60-
; CHECK-NEXT: ret <4 x float> [[INS2]]
60+
; CHECK-NEXT: ret <4 x float> <float undef, float undef, float 2.000000e+00, float undef>
6161
;
6262
%ins1 = insertelement<4 x float> %x, float 1.0, i32 5
6363
%ins2 = insertelement<4 x float> %ins1, float 2.0, i32 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: opt -S -instsimplify < %s | FileCheck %s
2+
3+
define <4 x i32> @test1(<4 x i32> %A) {
4+
%I = insertelement <4 x i32> %A, i32 5, i64 4294967296
5+
; CHECK: ret <4 x i32> undef
6+
ret <4 x i32> %I
7+
}
8+
9+
define <4 x i32> @test2(<4 x i32> %A) {
10+
%I = insertelement <4 x i32> %A, i32 5, i64 4
11+
; CHECK: ret <4 x i32> undef
12+
ret <4 x i32> %I
13+
}
14+
15+
define <4 x i32> @test3(<4 x i32> %A) {
16+
%I = insertelement <4 x i32> %A, i32 5, i64 1
17+
; CHECK: ret <4 x i32> %I
18+
ret <4 x i32> %I
19+
}
20+
21+
define <4 x i32> @test4(<4 x i32> %A) {
22+
%I = insertelement <4 x i32> %A, i32 5, i128 100
23+
; CHECK: ret <4 x i32> undef
24+
ret <4 x i32> %I
25+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
; RUN: opt -S -instsimplify < %s | FileCheck %s
2-
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
3-
target triple = "x86_64-pc-windows-msvc"
42
%S = type { i16, i32 }
53

64
define <2 x i16> @test1() {
@@ -9,5 +7,6 @@ entry:
97
ret <2 x i16> %b
108
}
119

10+
; InstCombine will be able to fold this into zeroinitializer
1211
; CHECK-LABEL: @test1(
13-
; CHECK: ret <2 x i16> zeroinitializer
12+
; CHECK: ret <2 x i16> <i16 extractvalue (%S select (i1 icmp eq (i16 extractelement (<2 x i16> bitcast (<1 x i32> <i32 1> to <2 x i16>), i32 0), i16 0), %S zeroinitializer, %S { i16 0, i32 1 }), 0), i16 0>

0 commit comments

Comments
 (0)