From a95a7105efde0cfb58ee92eb0f43646b38720b74 Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Thu, 24 Jan 2019 19:32:48 +0000 Subject: [PATCH] [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 llvm-svn: 352089 --- llvm/include/llvm/IR/IRBuilder.h | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h index e14864924fee..c7cfee6e093f 100644 --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/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);