llvm.org GIT mirror llvm / 35bda89
enhance vmcore to know that udiv's can be exact, and add a trivial instcombine xform to exercise this. Nothing forms exact udivs yet though. This is progress on PR8862 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@124992 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 9 years ago
15 changed file(s) with 162 addition(s) and 84 deletion(s). Raw diff Collapse all Expand all
34403440
34413441
Syntax:
34423442

                  
                
3443 <result> = udiv <ty> <op1>, <op2> ; yields {ty}:result
3443 <result> = udiv <ty> <op1>, <op2> ; yields {ty}:result
3444 <result> = udiv exact <ty> <op1>, <op2> ; yields {ty}:result
34443445
34453446
34463447
Overview:
34583459 operations; for signed integer division, use 'sdiv'.

34593460
34603461

Division by zero leads to undefined behavior.

3462
3463

If the exact keyword is present, the result value of the

3464 udiv is a trap value if %op1 is not a
3465 multiple of %op2 (as such, "((a udiv exact b) mul b) == a").

3466
34613467
34623468
Example:
34633469

                  
                
198198 OBO_NO_SIGNED_WRAP = 1
199199 };
200200
201 /// SDivOperatorOptionalFlags - Flags for serializing SDivOperator's
202 /// SubclassOptionalData contents.
203 enum SDivOperatorOptionalFlags {
204 SDIV_EXACT = 0
201 /// PossiblyExactOperatorOptionalFlags - Flags for serializing
202 /// PossiblyExactOperator's SubclassOptionalData contents.
203 enum PossiblyExactOperatorOptionalFlags {
204 PEO_EXACT = 0
205205 };
206206
207207 // The function body block (FUNCTION_BLOCK_ID) describes function bodies. It
724724 static Constant *getNSWMul(Constant *C1, Constant *C2);
725725 static Constant *getNUWMul(Constant *C1, Constant *C2);
726726 static Constant *getExactSDiv(Constant *C1, Constant *C2);
727 static Constant *getExactUDiv(Constant *C1, Constant *C2);
727728
728729 /// Transparently provide more efficient getOperand methods.
729730 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Constant);
340340 BO->setIsExact(true);
341341 return BO;
342342 }
343
343
344344 /// Helper functions to construct and inspect unary operations (NEG and NOT)
345345 /// via binary operators SUB and XOR:
346346 ///
172172 }
173173 };
174174
175 /// SDivOperator - An Operator with opcode Instruction::SDiv.
176 ///
177 class SDivOperator : public Operator {
175 /// PossiblyExactOperator - A udiv or sdiv instruction, which can be marked as
176 /// "exact", indicating that no bits are destroyed.
177 class PossiblyExactOperator : public Operator {
178178 public:
179179 enum {
180180 IsExact = (1 << 0)
181181 };
182
183 private:
184 ~SDivOperator(); // do not implement
185
182
186183 friend class BinaryOperator;
187184 friend class ConstantExpr;
188185 void setIsExact(bool B) {
189186 SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
190187 }
191
188
189 private:
190 ~PossiblyExactOperator(); // do not implement
192191 public:
193192 /// isExact - Test whether this division is known to be exact, with
194193 /// zero remainder.
195194 bool isExact() const {
196195 return SubclassOptionalData & IsExact;
197196 }
198
197
198 static inline bool classof(const ConstantExpr *CE) {
199 return CE->getOpcode() == Instruction::SDiv ||
200 CE->getOpcode() == Instruction::UDiv;
201 }
202 static inline bool classof(const Instruction *I) {
203 return I->getOpcode() == Instruction::SDiv ||
204 I->getOpcode() == Instruction::UDiv;
205 }
206 static inline bool classof(const Value *V) {
207 return (isa(V) && classof(cast(V))) ||
208 (isa(V) && classof(cast(V)));
209 }
210 };
211
212 /// SDivOperator - An Operator with opcode Instruction::SDiv.
213 ///
214 class SDivOperator : public PossiblyExactOperator {
215 public:
199216 // Methods for support type inquiry through isa, cast, and dyn_cast:
200217 static inline bool classof(const SDivOperator *) { return true; }
201218 static inline bool classof(const ConstantExpr *CE) {
210227 }
211228 };
212229
230 /// UDivOperator - An Operator with opcode Instruction::SDiv.
231 ///
232 class UDivOperator : public PossiblyExactOperator {
233 public:
234 // Methods for support type inquiry through isa, cast, and dyn_cast:
235 static inline bool classof(const UDivOperator *) { return true; }
236 static inline bool classof(const ConstantExpr *CE) {
237 return CE->getOpcode() == Instruction::UDiv;
238 }
239 static inline bool classof(const Instruction *I) {
240 return I->getOpcode() == Instruction::UDiv;
241 }
242 static inline bool classof(const Value *V) {
243 return (isa(V) && classof(cast(V))) ||
244 (isa(V) && classof(cast(V)));
245 }
246 };
247
213248 class GEPOperator : public Operator {
214249 enum {
215250 IsInBounds = (1 << 0)
23032303 if (EatIfPresent(lltok::kw_nuw))
23042304 NUW = true;
23052305 }
2306 } else if (Opc == Instruction::SDiv) {
2306 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv) {
23072307 if (EatIfPresent(lltok::kw_exact))
23082308 Exact = true;
23092309 }
23462346 unsigned Flags = 0;
23472347 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
23482348 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
2349 if (Exact) Flags |= SDivOperator::IsExact;
2349 if (Exact) Flags |= PossiblyExactOperator::IsExact;
23502350 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
23512351 ID.ConstantVal = C;
23522352 ID.Kind = ValID::t_Constant;
30313031 case lltok::kw_fsub:
30323032 case lltok::kw_fmul: return ParseArithmetic(Inst, PFS, KeywordVal, 2);
30333033
3034 case lltok::kw_sdiv: {
3034 case lltok::kw_sdiv:
3035 case lltok::kw_udiv: {
30353036 bool Exact = false;
30363037 if (EatIfPresent(lltok::kw_exact))
30373038 Exact = true;
30423043 return Result;
30433044 }
30443045
3045 case lltok::kw_udiv:
30463046 case lltok::kw_urem:
30473047 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
30483048 case lltok::kw_fdiv:
10891089 Flags |= OverflowingBinaryOperator::NoSignedWrap;
10901090 if (Record[3] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
10911091 Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
1092 } else if (Opc == Instruction::SDiv) {
1093 if (Record[3] & (1 << bitc::SDIV_EXACT))
1092 } else if (Opc == Instruction::SDiv ||
1093 Opc == Instruction::UDiv) {
1094 if (Record[3] & (1 << bitc::PEO_EXACT))
10941095 Flags |= SDivOperator::IsExact;
10951096 }
10961097 }
19041905 cast(I)->setHasNoSignedWrap(true);
19051906 if (Record[OpNum] & (1 << bitc::OBO_NO_UNSIGNED_WRAP))
19061907 cast(I)->setHasNoUnsignedWrap(true);
1907 } else if (Opc == Instruction::SDiv) {
1908 if (Record[OpNum] & (1 << bitc::SDIV_EXACT))
1908 } else if (Opc == Instruction::SDiv ||
1909 Opc == Instruction::UDiv) {
1910 if (Record[OpNum] & (1 << bitc::PEO_EXACT))
19091911 cast(I)->setIsExact(true);
19101912 }
19111913 }
469469 Flags |= 1 << bitc::OBO_NO_SIGNED_WRAP;
470470 if (OBO->hasNoUnsignedWrap())
471471 Flags |= 1 << bitc::OBO_NO_UNSIGNED_WRAP;
472 } else if (const SDivOperator *Div = dyn_cast(V)) {
473 if (Div->isExact())
474 Flags |= 1 << bitc::SDIV_EXACT;
472 } else if (const PossiblyExactOperator *PEO =
473 dyn_cast(V)) {
474 if (PEO->isExact())
475 Flags |= 1 << bitc::PEO_EXACT;
475476 }
476477
477478 return Flags;
134134 BO->getOpcode() == Instruction::SDiv)) {
135135 Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1);
136136
137 // If the division is exact, X % Y is zero.
138 if (SDivOperator *SDiv = dyn_cast(BO))
137 // If the division is exact, X % Y is zero, so we end up with X or -X.
138 if (PossiblyExactOperator *SDiv = dyn_cast(BO))
139139 if (SDiv->isExact()) {
140140 if (Op1BO == Op1C)
141141 return ReplaceInstUsesWith(I, Op0BO);
830830 Out << " nuw";
831831 if (OBO->hasNoSignedWrap())
832832 Out << " nsw";
833 } else if (const SDivOperator *Div = dyn_cast(U)) {
833 } else if (const PossiblyExactOperator *Div =
834 dyn_cast(U)) {
834835 if (Div->isExact())
835836 Out << " exact";
836837 } else if (const GEPOperator *GEP = dyn_cast(U)) {
682682
683683 Constant* ConstantExpr::getExactSDiv(Constant* C1, Constant* C2) {
684684 return getTy(C1->getType(), Instruction::SDiv, C1, C2,
685 SDivOperator::IsExact);
685 PossiblyExactOperator::IsExact);
686 }
687
688 Constant* ConstantExpr::getExactUDiv(Constant* C1, Constant* C2) {
689 return getTy(C1->getType(), Instruction::UDiv, C1, C2,
690 PossiblyExactOperator::IsExact);
686691 }
687692
688693 // Utility function for determining if a ConstantExpr is a CastOp or not. This
18211821 }
18221822
18231823 void BinaryOperator::setIsExact(bool b) {
1824 cast<SDivOperator>(this)->setIsExact(b);
1824 cast<PossiblyExactOperator>(this)->setIsExact(b);
18251825 }
18261826
18271827 bool BinaryOperator::hasNoUnsignedWrap() const {
18331833 }
18341834
18351835 bool BinaryOperator::isExact() const {
1836 return cast<SDivOperator>(this)->isExact();
1836 return cast<PossiblyExactOperator>(this)->isExact();
18371837 }
18381838
18391839 //===----------------------------------------------------------------------===//
102102 %z = sdiv i64 %x, %y
103103 ret i64 %z
104104 }
105
106 define i64 @udiv_exact(i64 %x, i64 %y) {
107 ; CHECK: %z = udiv exact i64 %x, %y
108 %z = udiv exact i64 %x, %y
109 ret i64 %z
110 }
111
112 define i64 @udiv_plain(i64 %x, i64 %y) {
113 ; CHECK: %z = udiv i64 %x, %y
114 %z = udiv i64 %x, %y
115 ret i64 %z
116 }
117
105118
106119 define i64* @gep_nw(i64* %p, i64 %x) {
107120 ; CHECK: %z = getelementptr inbounds i64* %p, i64 %x
135148 ret i64 sdiv exact (i64 ptrtoint (i64* @addr to i64), i64 91)
136149 }
137150
151 define i64 @udiv_exact_ce() {
152 ; CHECK: ret i64 udiv exact (i64 ptrtoint (i64* @addr to i64), i64 91)
153 ret i64 udiv exact (i64 ptrtoint (i64* @addr to i64), i64 91)
154 }
155
138156 define i64* @gep_nw_ce() {
139157 ; CHECK: ret i64* getelementptr inbounds (i64* @addr, i64 171)
140158 ret i64* getelementptr inbounds (i64* @addr, i64 171)
209227 ; CHECK: ret i64 mul nuw (i64 ptrtoint (i64* @addr to i64), i64 91)
210228 ret i64 mul nuw (i64 ptrtoint (i64* @addr to i64), i64 91)
211229 }
230
+0
-52
test/Transforms/InstCombine/exact-sdiv.ll less more
None ; RUN: opt < %s -instcombine -S | FileCheck %s
1
2 ; CHECK: define i32 @foo
3 ; CHECK: sdiv i32 %x, 8
4 define i32 @foo(i32 %x) {
5 %y = sdiv i32 %x, 8
6 ret i32 %y
7 }
8
9 ; CHECK: define i32 @bar
10 ; CHECK: ashr i32 %x, 3
11 define i32 @bar(i32 %x) {
12 %y = sdiv exact i32 %x, 8
13 ret i32 %y
14 }
15
16 ; CHECK: i32 @a0
17 ; CHECK: %y = srem i32 %x, 3
18 ; CHECK: %z = sub i32 %x, %y
19 ; CHECK: ret i32 %z
20 define i32 @a0(i32 %x) {
21 %y = sdiv i32 %x, 3
22 %z = mul i32 %y, 3
23 ret i32 %z
24 }
25
26 ; CHECK: i32 @b0
27 ; CHECK: ret i32 %x
28 define i32 @b0(i32 %x) {
29 %y = sdiv exact i32 %x, 3
30 %z = mul i32 %y, 3
31 ret i32 %z
32 }
33
34 ; CHECK: i32 @a1
35 ; CHECK: %y = srem i32 %x, 3
36 ; CHECK: %z = sub i32 %y, %x
37 ; CHECK: ret i32 %z
38 define i32 @a1(i32 %x) {
39 %y = sdiv i32 %x, 3
40 %z = mul i32 %y, -3
41 ret i32 %z
42 }
43
44 ; CHECK: i32 @b1
45 ; CHECK: %z = sub i32 0, %x
46 ; CHECK: ret i32 %z
47 define i32 @b1(i32 %x) {
48 %y = sdiv exact i32 %x, 3
49 %z = mul i32 %y, -3
50 ret i32 %z
51 }
0 ; RUN: opt < %s -instcombine -S | FileCheck %s
1
2 ; CHECK: define i32 @foo
3 ; CHECK: sdiv i32 %x, 8
4 define i32 @foo(i32 %x) {
5 %y = sdiv i32 %x, 8
6 ret i32 %y
7 }
8
9 ; CHECK: define i32 @bar
10 ; CHECK: ashr i32 %x, 3
11 define i32 @bar(i32 %x) {
12 %y = sdiv exact i32 %x, 8
13 ret i32 %y
14 }
15
16 ; CHECK: i32 @a0
17 ; CHECK: %y = srem i32 %x, 3
18 ; CHECK: %z = sub i32 %x, %y
19 ; CHECK: ret i32 %z
20 define i32 @a0(i32 %x) {
21 %y = sdiv i32 %x, 3
22 %z = mul i32 %y, 3
23 ret i32 %z
24 }
25
26 ; CHECK: i32 @b0
27 ; CHECK: ret i32 %x
28 define i32 @b0(i32 %x) {
29 %y = sdiv exact i32 %x, 3
30 %z = mul i32 %y, 3
31 ret i32 %z
32 }
33
34 ; CHECK: i32 @a1
35 ; CHECK: %y = srem i32 %x, 3
36 ; CHECK: %z = sub i32 %y, %x
37 ; CHECK: ret i32 %z
38 define i32 @a1(i32 %x) {
39 %y = sdiv i32 %x, 3
40 %z = mul i32 %y, -3
41 ret i32 %z
42 }
43
44 ; CHECK: i32 @b1
45 ; CHECK: %z = sub i32 0, %x
46 ; CHECK: ret i32 %z
47 define i32 @b1(i32 %x) {
48 %y = sdiv exact i32 %x, 3
49 %z = mul i32 %y, -3
50 ret i32 %z
51 }
52
53 ; CHECK: i32 @b2
54 ; CHECK: ret i32 %x
55 define i32 @b2(i32 %x, i32 %w) {
56 %y = udiv exact i32 %x, %w
57 %z = mul i32 %y, %w
58 ret i32 %z
59 }