Teach computeKnownBits to use new align attribute/metadata
authorArtur Pilipenko <apilipenko@azulsystems.com>
Wed, 7 Oct 2015 16:01:18 +0000 (16:01 +0000)
committerArtur Pilipenko <apilipenko@azulsystems.com>
Wed, 7 Oct 2015 16:01:18 +0000 (16:01 +0000)
Reviewed By: reames

Differential Revision: http://reviews.llvm.org/D13470

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@249557 91177308-0d34-0410-b5e6-96231b3b80d8

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

index d3723037ddaf4fd0204db4d345218319800c392e..d8a449193a3fe82f5eabb1738bd5810aadf47f73 100644 (file)
@@ -1415,7 +1415,7 @@ static void computeKnownBitsFromOperator(Operator *I, APInt &KnownZero,
   }
 }
 
-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();
@@ -1433,7 +1433,7 @@ static unsigned getAlignment(Value *V, const DataLayout &DL) {
         }
       }
     }
-  } 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()) {
@@ -1442,7 +1442,16 @@ static unsigned getAlignment(Value *V, const DataLayout &DL) {
       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;
 }
 
index 4b869ef2c50e1319a2d6137514a9db9838846a83..4bdbcc8d086aa400f3da13c15b453f41763db1fe 100644 (file)
@@ -47,6 +47,32 @@ for.end:                                          ; preds = %for.body
   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 @test1() {
+  %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
+}
+
+; Check that redundant align assume is removed
+; CHECK-LABEL: @test
+; CHECK-NOT: call void @llvm.assume
+define void @test3() {
+  %p = alloca i8, align 8
+  %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