Bitcasts are transitive. Bitcast-Bitcast-X becomes Bitcast-X.
authorNadav Rotem <nadav.rotem@intel.com>
Sun, 28 Aug 2011 11:51:08 +0000 (11:51 +0000)
committerNadav Rotem <nadav.rotem@intel.com>
Sun, 28 Aug 2011 11:51:08 +0000 (11:51 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@138722 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ConstantFolding.cpp
lib/Transforms/InstCombine/InstCombineCasts.cpp
test/Transforms/InstCombine/cast.ll

index df79849c3cf4f3681b61366cd1617f79421cb14e..613804d6d2d1588d6313c3f649c8080ebd79d490 100644 (file)
@@ -51,6 +51,12 @@ static Constant *FoldBitCast(Constant *C, Type *DestTy,
   if (C->isAllOnesValue() && !DestTy->isX86_MMXTy())
     return Constant::getAllOnesValue(DestTy);
 
+  // Bitcast of Bitcast can be done using a single cast.
+  ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
+  if (CE && CE->getOpcode() == Instruction::BitCast) {
+    return ConstantExpr::getBitCast(CE->getOperand(0), DestTy);
+  }
+
   // The code below only handles casts to vectors currently.
   VectorType *DestVTy = dyn_cast<VectorType>(DestTy);
   if (DestVTy == 0)
index ba90bf6b5c56b2ca82efbd3f0ba2218eca7cf74d..6c42c0c66003112a9b6c3ca68755d57fcd09c02a 100644 (file)
@@ -1659,6 +1659,11 @@ Instruction *InstCombiner::visitBitCast(BitCastInst &CI) {
   if (DestTy == Src->getType())
     return ReplaceInstUsesWith(CI, Src);
 
+  // Bitcasts are transitive.
+  if (BitCastInst* BSrc = dyn_cast<BitCastInst>(Src)) {
+    return CastInst::Create(Instruction::BitCast, BSrc->getOperand(0), DestTy);
+  }
+
   if (PointerType *DstPTy = dyn_cast<PointerType>(DestTy)) {
     PointerType *SrcPTy = cast<PointerType>(SrcTy);
     Type *DstElTy = DstPTy->getElementType();
index f85636f8df54de08b0b6735706ef83ea4ef8e423..06cdae24b49b37efd401d21f211e366d147ac5cd 100644 (file)
@@ -630,3 +630,17 @@ entry:
 ; CHECK: uitofp
 }
 
+define <4 x float> @test64(<4 x float> %c) nounwind {
+  %t0 = bitcast <4 x float> %c to <4 x i32>
+  %t1 = bitcast <4 x i32> %t0 to <2 x double>
+  %t2 = bitcast <2 x double> %t1 to <4 x float>
+  ret <4 x float> %t2
+; CHECK: @test64
+; CHECK-NEXT: ret <4 x float> %c
+}
+
+define float @test2c() {
+  ret float extractelement (<2 x float> bitcast (double bitcast (<2 x float> <float -1.000000e+00, float -1.000000e+00> to double) to <2 x float>), i32 0)
+; CHECK: @test2c
+; CHECK-NOT: extractelement
+}