Skip to content

Commit 835e7bc

Browse files
committed
Recommit r171461 which was incorrectly reverted. Mark DIV/IDIV instructions hasSideEffects=1 because they can trap when dividing by 0. This is needed to keep early if conversion from moving them across basic blocks.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171608 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 5d1f5c1 commit 835e7bc

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/Target/X86/X86InstrArithmetic.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def IMUL64rmi8 : RIi8<0x6B, MRMSrcMem, // GR64 = [mem64]*I8
266266

267267

268268
// unsigned division/remainder
269-
let hasSideEffects = 0 in {
269+
let hasSideEffects = 1 in { // so that we don't speculatively execute
270270
let Defs = [AL,EFLAGS,AX], Uses = [AX] in
271271
def DIV8r : I<0xF6, MRM6r, (outs), (ins GR8:$src), // AX/r8 = AL,AH
272272
"div{b}\t$src", [], IIC_DIV8_REG>;

test/CodeGen/X86/early-ifcvt.ll

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,34 @@ save_state_and_return:
142142
}
143143

144144
declare void @BZ2_bz__AssertH__fail()
145+
146+
; Make sure we don't speculate on div/idiv instructions
147+
; CHECK: test_idiv
148+
; CHECK-NOT: cmov
149+
define i32 @test_idiv(i32 %a, i32 %b) nounwind uwtable readnone ssp {
150+
%1 = icmp eq i32 %b, 0
151+
br i1 %1, label %4, label %2
152+
153+
; <label>:2 ; preds = %0
154+
%3 = sdiv i32 %a, %b
155+
br label %4
156+
157+
; <label>:4 ; preds = %0, %2
158+
%5 = phi i32 [ %3, %2 ], [ %a, %0 ]
159+
ret i32 %5
160+
}
161+
162+
; CHECK: test_div
163+
; CHECK-NOT: cmov
164+
define i32 @test_div(i32 %a, i32 %b) nounwind uwtable readnone ssp {
165+
%1 = icmp eq i32 %b, 0
166+
br i1 %1, label %4, label %2
167+
168+
; <label>:2 ; preds = %0
169+
%3 = udiv i32 %a, %b
170+
br label %4
171+
172+
; <label>:4 ; preds = %0, %2
173+
%5 = phi i32 [ %3, %2 ], [ %a, %0 ]
174+
ret i32 %5
175+
}

0 commit comments

Comments
 (0)