Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 3b637f6

Browse files
committed
Handle ConstantExpr correctly in SelectionDAGBuilder
This change fixes a bug in SelectionDAGBuilder::visitInsertValue and SelectionDAGBuilder::visitExtractValue where constant expressions (InsertValueConstantExpr and ExtractValueConstantExpr) would be treated as non-constant instructions (InsertValueInst and ExtractValueInst). This bug resulted in an incorrect memory access, which manifested as an assertion failure in SDValue::SDValue. Fixes PR#33094. Submitted on behalf of @Praetonus (Benoit Vey) Differential Revision: https://reviews.llvm.org/D34538 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@307502 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 2bd71f2 commit 3b637f6

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3220,15 +3220,21 @@ void SelectionDAGBuilder::visitShuffleVector(const User &I) {
32203220
setValue(&I, DAG.getBuildVector(VT, DL, Ops));
32213221
}
32223222

3223-
void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
3223+
void SelectionDAGBuilder::visitInsertValue(const User &I) {
3224+
ArrayRef<unsigned> Indices;
3225+
if (const InsertValueInst *IV = dyn_cast<InsertValueInst>(&I))
3226+
Indices = IV->getIndices();
3227+
else
3228+
Indices = cast<ConstantExpr>(&I)->getIndices();
3229+
32243230
const Value *Op0 = I.getOperand(0);
32253231
const Value *Op1 = I.getOperand(1);
32263232
Type *AggTy = I.getType();
32273233
Type *ValTy = Op1->getType();
32283234
bool IntoUndef = isa<UndefValue>(Op0);
32293235
bool FromUndef = isa<UndefValue>(Op1);
32303236

3231-
unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
3237+
unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
32323238

32333239
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
32343240
SmallVector<EVT, 4> AggValueVTs;
@@ -3268,13 +3274,19 @@ void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
32683274
DAG.getVTList(AggValueVTs), Values));
32693275
}
32703276

3271-
void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
3277+
void SelectionDAGBuilder::visitExtractValue(const User &I) {
3278+
ArrayRef<unsigned> Indices;
3279+
if (const ExtractValueInst *EV = dyn_cast<ExtractValueInst>(&I))
3280+
Indices = EV->getIndices();
3281+
else
3282+
Indices = cast<ConstantExpr>(&I)->getIndices();
3283+
32723284
const Value *Op0 = I.getOperand(0);
32733285
Type *AggTy = Op0->getType();
32743286
Type *ValTy = I.getType();
32753287
bool OutOfUndef = isa<UndefValue>(Op0);
32763288

3277-
unsigned LinearIndex = ComputeLinearIndex(AggTy, I.getIndices());
3289+
unsigned LinearIndex = ComputeLinearIndex(AggTy, Indices);
32783290

32793291
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
32803292
SmallVector<EVT, 4> ValValueVTs;

lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class BranchInst;
3838
class CallInst;
3939
class DbgValueInst;
4040
class ExtractElementInst;
41-
class ExtractValueInst;
4241
class FCmpInst;
4342
class FPExtInst;
4443
class FPToSIInst;
@@ -53,7 +52,6 @@ class IntToPtrInst;
5352
class IndirectBrInst;
5453
class InvokeInst;
5554
class InsertElementInst;
56-
class InsertValueInst;
5755
class Instruction;
5856
class LoadInst;
5957
class MachineBasicBlock;
@@ -859,8 +857,8 @@ class SelectionDAGBuilder {
859857
void visitInsertElement(const User &I);
860858
void visitShuffleVector(const User &I);
861859

862-
void visitExtractValue(const ExtractValueInst &I);
863-
void visitInsertValue(const InsertValueInst &I);
860+
void visitExtractValue(const User &I);
861+
void visitInsertValue(const User &I);
864862
void visitLandingPad(const LandingPadInst &I);
865863

866864
void visitGetElementPtr(const User &I);

test/CodeGen/Generic/pr33094.ll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: llc < %s
2+
3+
; PR33094
4+
; Make sure that a constant extractvalue doesn't cause a crash in
5+
; SelectionDAGBuilder::visitExtractValue.
6+
7+
%A = type {}
8+
%B = type {}
9+
%Tuple = type { i64 }
10+
11+
@A_Inst = global %A zeroinitializer
12+
@B_Inst = global %B zeroinitializer
13+
14+
define i64 @foo() {
15+
ret i64 extractvalue (%Tuple select (i1 icmp eq
16+
(%B* bitcast (%A* @A_Inst to %B*), %B* @B_Inst),
17+
%Tuple { i64 33 }, %Tuple { i64 42 }), 0)
18+
}

0 commit comments

Comments
 (0)