[ValueTracking][MemCpyOpt] avoid crash on inttoptr with vector pointer type (PR48075)
authorSanjay Patel <spatel@rotateright.com>
Sun, 22 Nov 2020 17:54:18 +0000 (12:54 -0500)
committerSanjay Patel <spatel@rotateright.com>
Sun, 22 Nov 2020 17:54:18 +0000 (12:54 -0500)
llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Transforms/MemCpyOpt/crash.ll

index bcf3511..90f8dff 100644 (file)
@@ -3610,12 +3610,13 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
 
   if (auto *CE = dyn_cast<ConstantExpr>(C)) {
     if (CE->getOpcode() == Instruction::IntToPtr) {
-      auto PS = DL.getPointerSizeInBits(
-          cast<PointerType>(CE->getType())->getAddressSpace());
-      return isBytewiseValue(
-          ConstantExpr::getIntegerCast(CE->getOperand(0),
-                                       Type::getIntNTy(Ctx, PS), false),
-          DL);
+      if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
+        unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
+        return isBytewiseValue(
+            ConstantExpr::getIntegerCast(CE->getOperand(0),
+                                         Type::getIntNTy(Ctx, BitWidth), false),
+            DL);
+      }
     }
   }
 
index f70f104..7363589 100644 (file)
@@ -83,3 +83,16 @@ define void @test2(i32 %cmd) nounwind {
   call void @llvm.memcpy.p0i8.p0i8.i64(i8* null, i8* undef, i64 20, i1 false) nounwind
   ret void
 }
+
+; https://llvm.org/PR48075
+
+@g = external global i16, align 1
+
+define void @inttoptr_constexpr_crash(<1 x i16*>* %p) {
+; CHECK-LABEL: @inttoptr_constexpr_crash(
+; CHECK-NEXT:    store <1 x i16*> inttoptr (<1 x i16> bitcast (<2 x i8> <i8 ptrtoint (i16* @g to i8), i8 ptrtoint (i16* @g to i8)> to <1 x i16>) to <1 x i16*>), <1 x i16*>* [[P:%.*]], align 1
+; CHECK-NEXT:    ret void
+;
+  store <1 x i16*> inttoptr (<1 x i16> bitcast (<2 x i8> <i8 ptrtoint (i16* @g to i8), i8 ptrtoint (i16* @g to i8)> to <1 x i16>) to <1 x i16*>), <1 x i16*>* %p, align 1
+  ret void
+}