Skip to content

Commit 20d1d08

Browse files
changpengaokblast
authored andcommitted
[InstCombine] Add the missing insertion point before IRBuilder instruction creation (llvm#165315)
Should set the insertion point appropriately before we create an instruction with IRBuilder. Fixes: SWDEV-562571
1 parent e592772 commit 20d1d08

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ static Value *simplifyShiftSelectingPackedElement(Instruction *I,
105105
if (~KnownShrBits.Zero != ShlAmt)
106106
return nullptr;
107107

108+
IRBuilderBase::InsertPointGuard Guard(IC.Builder);
109+
IC.Builder.SetInsertPoint(I);
108110
Value *ShrAmtZ =
109111
IC.Builder.CreateICmpEQ(ShrAmt, Constant::getNullValue(ShrAmt->getType()),
110112
ShrAmt->getName() + ".z");

llvm/test/Transforms/InstCombine/fold-selective-shift.ll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ define i16 @selective_shift_16(i32 %mask, i16 %upper, i16 %lower) {
2121
ret i16 %trunc
2222
}
2323

24+
; Will assert if InsertPoint is not set before creating an instruction
25+
; with IRBuilder
26+
define i16 @selective_shift_16_insertpt(i32 %mask, i16 %upper, i16 %lower) {
27+
; CHECK-LABEL: define i16 @selective_shift_16_insertpt(
28+
; CHECK-SAME: i32 [[MASK:%.*]], i16 [[UPPER:%.*]], i16 [[LOWER:%.*]]) {
29+
; CHECK-NEXT: [[MASK_BIT:%.*]] = and i32 [[MASK]], 16
30+
; CHECK-NEXT: [[MASK_BIT_Z:%.*]] = icmp eq i32 [[MASK_BIT]], 0
31+
; CHECK-NEXT: [[SEL_V:%.*]] = select i1 [[MASK_BIT_Z]], i16 [[LOWER]], i16 [[UPPER]]
32+
; CHECK-NEXT: [[ADD_ONE:%.*]] = add i16 [[SEL_V]], 1
33+
; CHECK-NEXT: ret i16 [[ADD_ONE]]
34+
;
35+
%mask.bit = and i32 %mask, 16
36+
%upper.zext = zext i16 %upper to i32
37+
%upper.shl = shl nuw i32 %upper.zext, 16
38+
%lower.zext = zext i16 %lower to i32
39+
%pack = or disjoint i32 %upper.shl, %lower.zext
40+
%sel = lshr i32 %pack, %mask.bit
41+
%add.one = add i32 %sel, 1
42+
%trunc = trunc i32 %add.one to i16
43+
ret i16 %trunc
44+
}
45+
2446
define i16 @selective_shift_16.commute(i32 %mask, i16 %upper, i16 %lower) {
2547
; CHECK-LABEL: define i16 @selective_shift_16.commute(
2648
; CHECK-SAME: i32 [[MASK:%.*]], i16 [[UPPER:%.*]], i16 [[LOWER:%.*]]) {

0 commit comments

Comments
 (0)