Index: test/Transforms/LoopUnroll/full-unroll-heuristics-cast.ll =================================================================== --- test/Transforms/LoopUnroll/full-unroll-heuristics-cast.ll +++ /dev/null @@ -1,97 +0,0 @@ -; RUN: opt < %s -S -loop-unroll -unroll-max-iteration-count-to-analyze=100 -unroll-dynamic-cost-savings-discount=1000 -unroll-threshold=10 -unroll-percent-dynamic-cost-saved-threshold=50 | FileCheck %s -target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" - -@known_constant = internal unnamed_addr constant [10 x i32] [i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1, i32 0, i32 1], align 16 - -; We should be able to propagate constant data through different types of -; casts. For example, in this test we have a load, which becomes constant after -; unrolling, which then is truncated to i8. Obviously, truncated value is also a -; constant, which can be used in the further simplifications. -; -; We expect this loop to be unrolled, because in this case load would become -; constant, which is 0 in many cases, and which, in its turn, helps to simplify -; following multiplication and addition. In total, unrolling should help to -; optimize ~60% of all instructions in this case. -; -; CHECK-LABEL: @const_load_trunc -; CHECK-NOT: br i1 -; CHECK: ret i8 % -define i8 @const_load_trunc(i32* noalias nocapture readonly %src) { -entry: - br label %loop - -loop: ; preds = %loop, %entry - %iv = phi i64 [ 0, %entry ], [ %inc, %loop ] - %r = phi i8 [ 0, %entry ], [ %add, %loop ] - %arrayidx = getelementptr inbounds i32, i32* %src, i64 %iv - %src_element = load i32, i32* %arrayidx, align 4 - %array_const_idx = getelementptr inbounds [10 x i32], [10 x i32]* @known_constant, i64 0, i64 %iv - %const_array_element = load i32, i32* %array_const_idx, align 4 - %x = trunc i32 %src_element to i8 - %y = trunc i32 %const_array_element to i8 - %mul = mul nsw i8 %x, %y - %add = add nsw i8 %mul, %r - %inc = add nuw nsw i64 %iv, 1 - %exitcond86.i = icmp eq i64 %inc, 10 - br i1 %exitcond86.i, label %loop.end, label %loop - -loop.end: ; preds = %loop - %r.lcssa = phi i8 [ %r, %loop ] - ret i8 %r.lcssa -} - -; The same test as before, but with ZEXT instead of TRUNC. -; CHECK-LABEL: @const_load_zext -; CHECK-NOT: br i1 -; CHECK: ret i64 % -define i64 @const_load_zext(i32* noalias nocapture readonly %src) { -entry: - br label %loop - -loop: ; preds = %loop, %entry - %iv = phi i64 [ 0, %entry ], [ %inc, %loop ] - %r = phi i64 [ 0, %entry ], [ %add, %loop ] - %arrayidx = getelementptr inbounds i32, i32* %src, i64 %iv - %src_element = load i32, i32* %arrayidx, align 4 - %array_const_idx = getelementptr inbounds [10 x i32], [10 x i32]* @known_constant, i64 0, i64 %iv - %const_array_element = load i32, i32* %array_const_idx, align 4 - %x = zext i32 %src_element to i64 - %y = zext i32 %const_array_element to i64 - %mul = mul nsw i64 %x, %y - %add = add nsw i64 %mul, %r - %inc = add nuw nsw i64 %iv, 1 - %exitcond86.i = icmp eq i64 %inc, 10 - br i1 %exitcond86.i, label %loop.end, label %loop - -loop.end: ; preds = %loop - %r.lcssa = phi i64 [ %r, %loop ] - ret i64 %r.lcssa -} - -; The same test as the first one, but with SEXT instead of TRUNC. -; CHECK-LABEL: @const_load_sext -; CHECK-NOT: br i1 -; CHECK: ret i64 % -define i64 @const_load_sext(i32* noalias nocapture readonly %src) { -entry: - br label %loop - -loop: ; preds = %loop, %entry - %iv = phi i64 [ 0, %entry ], [ %inc, %loop ] - %r = phi i64 [ 0, %entry ], [ %add, %loop ] - %arrayidx = getelementptr inbounds i32, i32* %src, i64 %iv - %src_element = load i32, i32* %arrayidx, align 4 - %array_const_idx = getelementptr inbounds [10 x i32], [10 x i32]* @known_constant, i64 0, i64 %iv - %const_array_element = load i32, i32* %array_const_idx, align 4 - %x = sext i32 %src_element to i64 - %y = sext i32 %const_array_element to i64 - %mul = mul nsw i64 %x, %y - %add = add nsw i64 %mul, %r - %inc = add nuw nsw i64 %iv, 1 - %exitcond86.i = icmp eq i64 %inc, 10 - br i1 %exitcond86.i, label %loop.end, label %loop - -loop.end: ; preds = %loop - %r.lcssa = phi i64 [ %r, %loop ] - ret i64 %r.lcssa -} Index: test/Transforms/LoopUnroll/full-unroll-heuristics-cmp.ll =================================================================== --- test/Transforms/LoopUnroll/full-unroll-heuristics-cmp.ll +++ test/Transforms/LoopUnroll/full-unroll-heuristics-cmp.ll @@ -3,39 +3,6 @@ @known_constant = internal unnamed_addr constant [10 x i32] [i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1], align 16 -; We should be able to propagate constant data through comparisons. -; For example, in this test we have a load, which becomes constant after -; unrolling, making comparison with 0 also known to be 0 (false) - and that -; will trigger further simplifications. -; -; We expect this loop to be unrolled, because in this case load would become -; constant, which is always 1, and which, in its turn, helps to simplify -; following comparison, zero-extension, and addition. In total, unrolling should help to -; optimize more than 50% of all instructions in this case. -; -; CHECK-LABEL: @const_compare -; CHECK-NOT: br i1 % -; CHECK: ret i32 -define i32 @const_compare(i32* noalias nocapture readonly %b) { -entry: - br label %for.body - -for.body: ; preds = %for.inc, %entry - %iv.0 = phi i64 [ 0, %entry ], [ %iv.1, %for.body ] - %r.0 = phi i32 [ 0, %entry ], [ %r.1, %for.body ] - %arrayidx1 = getelementptr inbounds [10 x i32], [10 x i32]* @known_constant, i64 0, i64 %iv.0 - %x1 = load i32, i32* %arrayidx1, align 4 - %cmp = icmp eq i32 %x1, 0 - %cast = zext i1 %cmp to i32 - %iv.1 = add nuw nsw i64 %iv.0, 1 - %r.1 = add i32 %r.0, %cast - %exitcond = icmp eq i64 %iv.1, 10 - br i1 %exitcond, label %for.end, label %for.body - -for.end: ; preds = %for.inc - ret i32 %r.1 -} - ; If we can figure out result of comparison on each iteration, we can resolve ; the depending branch. That means, that the unrolled version of the loop would ; have less code, because we don't need not-taken basic blocks there. @@ -73,70 +40,6 @@ ret i32 %r.1 } -; This test is similar to the previous one, but in this we use IV in comparison -; (not a loaded value as we did there). -; CHECK-LABEL: @branch_iv -; CHECK-NOT: br i1 % -; CHECK: ret i64 -define i64 @branch_iv(i64* noalias nocapture readonly %b) { -entry: - br label %for.body - -for.body: ; preds = %for.inc, %entry - %indvars.iv = phi i64 [ 0, %entry ], [ %tmp3, %for.inc ] - %r.030 = phi i64 [ 0, %entry ], [ %r.1, %for.inc ] - %cmp3 = icmp eq i64 %indvars.iv, 5 - %tmp3 = add nuw nsw i64 %indvars.iv, 1 - br i1 %cmp3, label %if.then, label %for.inc - -if.then: ; preds = %for.body - %arrayidx2 = getelementptr inbounds i64, i64* %b, i64 %tmp3 - %tmp1 = load i64, i64* %arrayidx2, align 4 - %add = add nsw i64 %tmp1, %r.030 - br label %for.inc - -for.inc: ; preds = %if.then, %for.body - %r.1 = phi i64 [ %add, %if.then ], [ %r.030, %for.body ] - %exitcond = icmp eq i64 %tmp3, 20 - br i1 %exitcond, label %for.end, label %for.body - -for.end: ; preds = %for.inc - ret i64 %r.1 -} - -; Induction variables are often casted to another type, and that shouldn't -; prevent us from folding branches. Tthis test specifically checks if we can -; handle this. Other than thatm it's similar to the previous test. -; CHECK-LABEL: @branch_iv_trunc -; CHECK-NOT: br i1 % -; CHECK: ret i32 -define i32 @branch_iv_trunc(i32* noalias nocapture readonly %b) { -entry: - br label %for.body - -for.body: ; preds = %for.inc, %entry - %indvars.iv = phi i64 [ 0, %entry ], [ %tmp3, %for.inc ] - %r.030 = phi i32 [ 0, %entry ], [ %r.1, %for.inc ] - %tmp2 = trunc i64 %indvars.iv to i32 - %cmp3 = icmp eq i32 %tmp2, 5 - %tmp3 = add nuw nsw i64 %indvars.iv, 1 - br i1 %cmp3, label %if.then, label %for.inc - -if.then: ; preds = %for.body - %arrayidx2 = getelementptr inbounds i32, i32* %b, i64 %tmp3 - %tmp1 = load i32, i32* %arrayidx2, align 4 - %add = add nsw i32 %tmp1, %r.030 - br label %for.inc - -for.inc: ; preds = %if.then, %for.body - %r.1 = phi i32 [ %add, %if.then ], [ %r.030, %for.body ] - %exitcond = icmp eq i64 %tmp3, 10 - br i1 %exitcond, label %for.end, label %for.body - -for.end: ; preds = %for.inc - ret i32 %r.1 -} - ; Check that we don't crash when we analyze icmp with pointer-typed IV and a ; pointer. ; CHECK-LABEL: @ptr_cmp_crash @@ -173,35 +76,3 @@ loop.exit: ret void } - -; Loop unroller should be able to predict that a comparison would become -; constant if the operands are pointers with the same base and constant -; offsets. -; We expect this loop to be unrolled, since most of its instructions would -; become constant after it. -; CHECK-LABEL: @ptr_cmp -; CHECK-NOT: br i1 % -; CHECK: ret i64 -define i64 @ptr_cmp(i8 * %a) { -entry: - %limit = getelementptr i8, i8* %a, i64 40 - %start.iv2 = getelementptr i8, i8* %a, i64 7 - br label %loop.body - -loop.body: - %iv.0 = phi i8* [ %a, %entry ], [ %iv.1, %loop.body ] - %iv2.0 = phi i8* [ %start.iv2, %entry ], [ %iv2.1, %loop.body ] - %r.0 = phi i64 [ 0, %entry ], [ %r.1, %loop.body ] - %cast = ptrtoint i8* %iv.0 to i64 - %cmp = icmp eq i8* %iv2.0, %iv.0 - %sub = sext i1 %cmp to i64 - %mul = mul i64 %sub, %cast - %r.1 = add i64 %r.0, %mul - %iv.1 = getelementptr inbounds i8, i8* %iv.0, i64 1 - %iv2.1 = getelementptr inbounds i8, i8* %iv2.0, i64 1 - %exitcond = icmp ne i8* %iv.1, %limit - br i1 %exitcond, label %loop.body, label %loop.exit - -loop.exit: - ret i64 %r.1 -} Index: unittests/Analysis/UnrollAnalyzer.cpp =================================================================== --- unittests/Analysis/UnrollAnalyzer.cpp +++ unittests/Analysis/UnrollAnalyzer.cpp @@ -171,6 +171,146 @@ auto I2 = SimplifiedValuesVector[0].find(Y2); EXPECT_TRUE(I2 == SimplifiedValuesVector[0].end()); } +TEST(UnrollAnalyzerTest, CmpSimplifications) { + const char *ModuleStr = + "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n" + "define void @branch_iv_trunc() {\n" + "entry:\n" + " br label %for.body\n" + "for.body:\n" + " %indvars.iv = phi i64 [ 0, %entry ], [ %tmp3, %for.body ]\n" + " %tmp2 = trunc i64 %indvars.iv to i32\n" + " %cmp3 = icmp eq i32 %tmp2, 5\n" + " %tmp3 = add nuw nsw i64 %indvars.iv, 1\n" + " %exitcond = icmp eq i64 %tmp3, 10\n" + " br i1 %exitcond, label %for.end, label %for.body\n" + "for.end:\n" + " ret void\n" + "}\n"; + UnrollAnalyzerTest *P = new UnrollAnalyzerTest(); + std::unique_ptr M = makeLLVMModule(P, ModuleStr); + legacy::PassManager Passes; + Passes.add(P); + Passes.run(*M); + + // Perform checks + Module::iterator MI = M->begin(); + Function *F = &*MI++; + Function::iterator FI = F->begin(); + FI++; // First basic block is entry - skip it. + BasicBlock *Header = &*FI++; + + BasicBlock::iterator BBI = Header->begin(); + BBI++; + Instruction *Y1 = &*BBI++; + Instruction *Y2 = &*BBI++; + // Check simplification expected on the 5th iteration. + // Check that "%tmp2 = trunc i64 %indvars.iv to i32" is simplified to 5 + // and "%cmp3 = icmp eq i32 %tmp2, 5" is simplified to 1 (i.e. true). + auto I1 = SimplifiedValuesVector[5].find(Y1); + EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end()); + EXPECT_EQ(dyn_cast((*I1).second)->getZExtValue(), 5U); + auto I2 = SimplifiedValuesVector[5].find(Y2); + EXPECT_TRUE(I2 != SimplifiedValuesVector[5].end()); + EXPECT_EQ(dyn_cast((*I2).second)->getZExtValue(), 1U); +} +TEST(UnrollAnalyzerTest, PtrCmpSimplifications) { + const char *ModuleStr = + "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n" + "define void @ptr_cmp(i8 *%a) {\n" + "entry:\n" + " %limit = getelementptr i8, i8* %a, i64 40\n" + " %start.iv2 = getelementptr i8, i8* %a, i64 7\n" + " br label %loop.body\n" + "loop.body:\n" + " %iv.0 = phi i8* [ %a, %entry ], [ %iv.1, %loop.body ]\n" + " %iv2.0 = phi i8* [ %start.iv2, %entry ], [ %iv2.1, %loop.body ]\n" + " %cmp = icmp eq i8* %iv2.0, %iv.0\n" + " %iv.1 = getelementptr inbounds i8, i8* %iv.0, i64 1\n" + " %iv2.1 = getelementptr inbounds i8, i8* %iv2.0, i64 1\n" + " %exitcond = icmp ne i8* %iv.1, %limit\n" + " br i1 %exitcond, label %loop.body, label %loop.exit\n" + "loop.exit:\n" + " ret void\n" + "}\n"; + UnrollAnalyzerTest *P = new UnrollAnalyzerTest(); + std::unique_ptr M = makeLLVMModule(P, ModuleStr); + legacy::PassManager Passes; + Passes.add(P); + Passes.run(*M); + + // Perform checks + Module::iterator MI = M->begin(); + Function *F = &*MI++; + Function::iterator FI = F->begin(); + FI++; // First basic block is entry - skip it. + BasicBlock *Header = &*FI; + + BasicBlock::iterator BBI = Header->begin(); + std::advance(BBI, 2); + Instruction *Y1 = &*BBI; + // Check simplification expected on the 5th iteration. + // Check that "%cmp = icmp eq i8* %iv2.0, %iv.0" is simplified to 0. + auto I1 = SimplifiedValuesVector[5].find(Y1); + EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end()); + EXPECT_EQ(dyn_cast((*I1).second)->getZExtValue(), 0U); +} +TEST(UnrollAnalyzerTest, CastSimplifications) { + const char *ModuleStr = + "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n" + "@known_constant = internal unnamed_addr constant [10 x i32] [i32 0, i32 1, i32 0, i32 1, i32 0, i32 259, i32 0, i32 1, i32 0, i32 1], align 16\n" + "define void @const_load_cast() {\n" + "entry:\n" + " br label %loop\n" + "\n" + "loop:\n" + " %iv = phi i64 [ 0, %entry ], [ %inc, %loop ]\n" + " %array_const_idx = getelementptr inbounds [10 x i32], [10 x i32]* @known_constant, i64 0, i64 %iv\n" + " %const_array_element = load i32, i32* %array_const_idx, align 4\n" + " %se = sext i32 %const_array_element to i64\n" + " %ze = zext i32 %const_array_element to i64\n" + " %tr = trunc i32 %const_array_element to i8\n" + " %inc = add nuw nsw i64 %iv, 1\n" + " %exitcond86.i = icmp eq i64 %inc, 10\n" + " br i1 %exitcond86.i, label %loop.end, label %loop\n" + "\n" + "loop.end:\n" + " ret void\n" + "}\n"; + + UnrollAnalyzerTest *P = new UnrollAnalyzerTest(); + std::unique_ptr M = makeLLVMModule(P, ModuleStr); + legacy::PassManager Passes; + Passes.add(P); + Passes.run(*M); + + // Perform checks + Module::iterator MI = M->begin(); + Function *F = &*MI++; + Function::iterator FI = F->begin(); + FI++; // First basic block is entry - skip it. + BasicBlock *Header = &*FI++; + + BasicBlock::iterator BBI = Header->begin(); + std::advance(BBI, 3); + Instruction *Y1 = &*BBI++; + Instruction *Y2 = &*BBI++; + Instruction *Y3 = &*BBI++; + // Check simplification expected on the 5th iteration. + // "%se = sext i32 %const_array_element to i64" should be simplified to 259, + // "%ze = zext i32 %const_array_element to i64" should be simplified to 259, + // "%tr = trunc i32 %const_array_element to i8" should be simplified to 3. + auto I1 = SimplifiedValuesVector[5].find(Y1); + EXPECT_TRUE(I1 != SimplifiedValuesVector[5].end()); + EXPECT_EQ(dyn_cast((*I1).second)->getZExtValue(), 259U); + auto I2 = SimplifiedValuesVector[5].find(Y2); + EXPECT_TRUE(I2 != SimplifiedValuesVector[5].end()); + EXPECT_EQ(dyn_cast((*I2).second)->getZExtValue(), 259U); + auto I3 = SimplifiedValuesVector[5].find(Y3); + EXPECT_TRUE(I3 != SimplifiedValuesVector[5].end()); + EXPECT_EQ(dyn_cast((*I3).second)->getZExtValue(), 3U); +} + } // end namespace llvm INITIALIZE_PASS_BEGIN(UnrollAnalyzerTest, "unrollanalyzertestpass",