From: Roman Lebedev Date: Thu, 24 Jan 2019 19:32:48 +0000 (+0000) Subject: [IRBuilder] Remove positivity check from CreateAlignmentAssumption() X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dea0eae9a50dc3ae9132d5d751683edb3c209bd2;p=llvm [IRBuilder] Remove positivity check from CreateAlignmentAssumption() Summary: An alignment should be non-zero positive power-of-two, anything and everything else is UB. We should not have that check for all these prerequisites here, it's just UB. Also, that was likely confusing middle-end passes. While there, `CreateIntCast()` should be called with `/*isSigned*/ false`. Think about it, there are two explanations: "An alignment should be positive", therefore the sign bit is unset, so `zext` and `sext` is equivalent. Or a second one: you have `i2 0b10` - a valid alignment, now you `sext` it: `i2 0b110` - no longer valid alignment. Reviewers: craig.topper, jyknight, hfinkel, erichkeane, rjmccall Reviewed By: hfinkel, rjmccall Subscribers: hfinkel, llvm-commits Differential Revision: https://reviews.llvm.org/D54653 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@352089 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/IR/IRBuilder.h b/include/llvm/IR/IRBuilder.h index e14864924fe..c7cfee6e093 100644 --- a/include/llvm/IR/IRBuilder.h +++ b/include/llvm/IR/IRBuilder.h @@ -2279,10 +2279,11 @@ public: Value **TheCheck = nullptr) { assert(isa(PtrValue->getType()) && "trying to create an alignment assumption on a non-pointer?"); + assert(Alignment != 0 && "Invalid Alignment"); auto *PtrTy = cast(PtrValue->getType()); Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace()); - Value *Mask = ConstantInt::get(IntPtrTy, Alignment > 0 ? Alignment - 1 : 0); + Value *Mask = ConstantInt::get(IntPtrTy, Alignment - 1); return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy, OffsetValue, TheCheck); } @@ -2309,15 +2310,10 @@ public: Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace()); if (Alignment->getType() != IntPtrTy) - Alignment = CreateIntCast(Alignment, IntPtrTy, /*isSigned*/ true, + Alignment = CreateIntCast(Alignment, IntPtrTy, /*isSigned*/ false, "alignmentcast"); - Value *IsPositive = - CreateICmp(CmpInst::ICMP_SGT, Alignment, - ConstantInt::get(Alignment->getType(), 0), "ispositive"); - Value *PositiveMask = - CreateSub(Alignment, ConstantInt::get(IntPtrTy, 1), "positivemask"); - Value *Mask = CreateSelect(IsPositive, PositiveMask, - ConstantInt::get(IntPtrTy, 0), "mask"); + + Value *Mask = CreateSub(Alignment, ConstantInt::get(IntPtrTy, 1), "mask"); return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy, OffsetValue, TheCheck);