teach sext optimization to handle truncs from types that are not
authorChris Lattner <sabre@nondot.org>
Sun, 10 Jan 2010 20:30:41 +0000 (20:30 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 10 Jan 2010 20:30:41 +0000 (20:30 +0000)
the dest of the sext.

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

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

index e2e74c1e5d542bde189d87bdd23766508c66ba11..49b2e22a6815424eca406ea808bfeb7b63447730 100644 (file)
@@ -799,6 +799,10 @@ static bool CanEvaluateSExtd(Value *V, const Type *Ty, TargetData *TD) {
   if (!I->hasOneUse()) return false;
 
   switch (I->getOpcode()) {
+  case Instruction::SExt:  // sext(sext(x)) -> sext(x)
+  case Instruction::ZExt:  // sext(zext(x)) -> zext(x)
+  case Instruction::Trunc: // sext(trunc(x)) -> trunc(x) or sext(x)
+    return true;
   case Instruction::And:
   case Instruction::Or:
   case Instruction::Xor:
@@ -813,9 +817,6 @@ static bool CanEvaluateSExtd(Value *V, const Type *Ty, TargetData *TD) {
   //case Instruction::LShr:  TODO
   //case Instruction::Trunc: TODO
       
-  case Instruction::SExt: // sext(sext(x)) -> sext(x)
-  case Instruction::ZExt: // sext(zext(x)) -> zext(x)
-    return true;
   case Instruction::Select:
     return CanEvaluateSExtd(I->getOperand(1), Ty, TD) &&
            CanEvaluateSExtd(I->getOperand(2), Ty, TD);
index 8424967e11ab58ca7317d59045134e7d582c973f..d624f342b9be452fe265ebc34ddcf610c7a572b5 100644 (file)
@@ -523,3 +523,29 @@ define i64 @test53(i32 %A) {
 ; CHECK-NEXT: %D = and i64 %C, 40186
 ; CHECK-NEXT: ret i64 %D
 }
+
+define i32 @test54(i64 %A) {
+  %B = trunc i64 %A to i16
+  %C = or i16 %B, -32574
+  %D = and i16 %C, -25350
+  %E = sext i16 %D to i32
+  ret i32 %E
+; CHECK: @test54
+; CHECK-NEXT: %B = trunc i64 %A to i32
+; CHECK-NEXT: %C = or i32 %B, -32574
+; CHECK-NEXT: %D = and i32 %C, -25350
+; CHECK-NEXT: ret i32 %D
+}
+
+define i64 @test55(i32 %A) {
+  %B = trunc i32 %A to i16
+  %C = or i16 %B, -32574
+  %D = and i16 %C, -25350
+  %E = sext i16 %D to i64
+  ret i64 %E
+; CHECK: @test55
+; CHECK-NEXT: %B = zext i32 %A to i64
+; CHECK-NEXT: %C = or i64 %B, -32574
+; CHECK-NEXT: %D = and i64 %C, -25350
+; CHECK-NEXT: ret i64 %D
+}
\ No newline at end of file