Hi, I am not able to understand the No Signed Wrap property. My problem is in the Instruction combiner which combines two operations - add1 = add 'nsw' x 5 add2 = add 'nsw' add1 1 into add2 = add x 6. // No 'nsw' property in the combined operation.>From the comments in the Instruction Combiner I can see that the nsw flag /property is "conservatively cleared" but do not understand why - especially when it was present in both the add instructions. Any insight would be appreciated. Thanks in advance, Pranav -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110619/e7fec279/attachment.html>
Hi Pranav,> I am not able to understand the No Signed Wrap property. My problem is in the > Instruction combiner which combines two operations - > add1 = add 'nsw' x 5 > add2 = add 'nsw' add1 1 > into > add2 = add x 6. // No 'nsw' property in the combined operation. > > From the comments in the Instruction Combiner I can see that the nsw flag / > property is "conservatively cleared" but do not understand why - especially when > it was present in both the add instructions.consider the following example: %add1 = add nsw i8 %x, 120 %add2 = add nsw i8 %add1, 10 Suppose for example that %x = -20. Then indeed there is no signed wrap in either addition, and the result is 110. The transform you mentioned would convert this into: %add2 = add i8 %x, -126 Notice how when %x = -20 there is now a signed wrap in the addition. Ciao, Duncan.
On 19 June 2011 15:46, Duncan Sands <baldrick at free.fr> wrote:>> I am not able to understand the No Signed Wrap property. My problem is in the >> Instruction combiner which combines two operations - >> add1 = add 'nsw' x 5 >> add2 = add 'nsw' add1 1 >> into >> add2 = add x 6. // No 'nsw' property in the combined operation. >> >> From the comments in the Instruction Combiner I can see that the nsw flag / >> property is "conservatively cleared" but do not understand why - especially when >> it was present in both the add instructions. > > consider the following example: > > %add1 = add nsw i8 %x, 120 > %add2 = add nsw i8 %add1, 10 > > Suppose for example that %x = -20. Then indeed there is no signed wrap in > either addition, and the result is 110. > > The transform you mentioned would convert this into: > > %add2 = add i8 %x, -126 > > Notice how when %x = -20 there is now a signed wrap in the addition.So there are (at least) two conditions: a) Both adds must be NSW. b) Adding their constant operands must itself be NSW. Any others? Because (b) should be pretty easy to check for, at least if both constant operands are ConstantInts like in these examples.