[PATCH] D13470: Teach computeKnownBits to use new align attribute/metadata

Artur Pilipenko via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 6 06:33:44 PDT 2015


apilipenko created this revision.
apilipenko added a reviewer: reames.
apilipenko added a subscriber: llvm-commits.

Teach computeKnownBits to use align attribute on return values and align metadata on loads. This fixes the following bug: https://llvm.org/bugs/show_bug.cgi?id=25068

http://reviews.llvm.org/D13470

Files:
  lib/Analysis/ValueTracking.cpp
  test/Transforms/InstCombine/assume-redundant.ll

Index: test/Transforms/InstCombine/assume-redundant.ll
===================================================================
--- test/Transforms/InstCombine/assume-redundant.ll
+++ test/Transforms/InstCombine/assume-redundant.ll
@@ -47,6 +47,20 @@
   ret void
 }
 
+declare align 8 i8* @get()
+
+; Check that redundant align assume is removed
+; CHECK-LABEL: @test
+; CHECK-NOT: call void @llvm.assume
+define void @test() {
+  %p = call align 8 i8* @get()
+  %ptrint = ptrtoint i8* %p to i64
+  %maskedptr = and i64 %ptrint, 7
+  %maskcond = icmp eq i64 %maskedptr, 0
+  call void @llvm.assume(i1 %maskcond)
+  ret void
+}
+
 ; Function Attrs: nounwind
 declare void @llvm.assume(i1) #1
 
Index: lib/Analysis/ValueTracking.cpp
===================================================================
--- lib/Analysis/ValueTracking.cpp
+++ lib/Analysis/ValueTracking.cpp
@@ -1409,7 +1409,7 @@
   }
 }
 
-static unsigned getAlignment(Value *V, const DataLayout &DL) {
+static unsigned getAlignment(const Value *V, const DataLayout &DL) {
   unsigned Align = 0;
   if (auto *GO = dyn_cast<GlobalObject>(V)) {
     Align = GO->getAlignment();
@@ -1427,16 +1427,25 @@
         }
       }
     }
-  } else if (Argument *A = dyn_cast<Argument>(V)) {
+  } else if (const Argument *A = dyn_cast<Argument>(V)) {
     Align = A->getType()->isPointerTy() ? A->getParamAlignment() : 0;
 
     if (!Align && A->hasStructRetAttr()) {
       // An sret parameter has at least the ABI alignment of the return type.
       Type *EltTy = cast<PointerType>(A->getType())->getElementType();
       if (EltTy->isSized())
         Align = DL.getABITypeAlignment(EltTy);
     }
-  }
+  } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V))
+    Align = AI->getAlignment();
+  else if (auto CS = ImmutableCallSite(V))
+    Align = CS.getAttributes().getParamAlignment(AttributeSet::ReturnIndex);
+  else if (const LoadInst *LI = dyn_cast<LoadInst>(V))
+    if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) {
+      ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
+      Align = CI->getLimitedValue();
+    }
+
   return Align;
 }
 
@@ -2975,20 +2984,7 @@
 
 static bool isAligned(const Value *Base, APInt Offset, unsigned Align,
                       const DataLayout &DL) {
-  APInt BaseAlign(Offset.getBitWidth(), 0);
-  if (const AllocaInst *AI = dyn_cast<AllocaInst>(Base))
-    BaseAlign = AI->getAlignment();
-  else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base))
-    BaseAlign = GV->getAlignment();
-  else if (const Argument *A = dyn_cast<Argument>(Base))
-    BaseAlign = A->getParamAlignment();
-  else if (auto CS = ImmutableCallSite(Base))
-    BaseAlign = CS.getAttributes().getParamAlignment(AttributeSet::ReturnIndex);
-  else if (const LoadInst *LI = dyn_cast<LoadInst>(Base))
-    if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) {
-      ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
-      BaseAlign = CI->getLimitedValue();
-    }
+  APInt BaseAlign(Offset.getBitWidth(), getAlignment(Base, DL));
 
   if (!BaseAlign) {
     Type *Ty = Base->getType()->getPointerElementType();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D13470.36618.patch
Type: text/x-patch
Size: 3148 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151006/51bfa40f/attachment.bin>


More information about the llvm-commits mailing list