1 //===-- tests/test_cfg_analysis.cpp -----------------------------*- C++ -*-===//
2 // Test of the loop depth detection.
4 // Copyright (C) 2013-2014 Dirk Steinke.
5 // See copyright and license notice in COPYRIGHT or include/jitcs.h
6 //===----------------------------------------------------------------------===//
10 #include "jitcs_instructionstream.h"
11 #include "jitcs_dumper.h"
12 #include "jitcs_int_cfg_analysis.h"
16 using namespace jitcs
;
18 static void test(UnitTest
& t
) {
19 typedef void (__cdecl
*FT0_c
)();
20 RefCounter
<IMachineInfo
> mi
= host::GetMachineInfo();
21 RefCounter
<TempAllocator
> alloc(new TempAllocator
);
23 std::unique_ptr
<Function
> fn
= mi
->createFnc
<FT0_c
>(alloc
);
24 InstructionStreamBuffer
<256> ibuf
;
26 Ref
<BasicBlock
> bb1
= fn
->getStartBlock();
27 Ref
<BasicBlock
> bb2
= fn
->createBasicBlock();
28 Ref
<BasicBlock
> bb3
= fn
->createBasicBlock();
29 Ref
<BasicBlock
> bb4
= fn
->createBasicBlock();
30 Ref
<BasicBlock
> bb5
= fn
->createBasicBlock();
31 Ref
<BasicBlock
> bb6
= fn
->createBasicBlock();
34 host::JGE_BB_FT(ibuf
, bb2
, bb3
);
42 host::JGE_BB_FT(ibuf
, bb4
, bb5
);
46 host::JMP_BB(ibuf
, bb6
);
50 host::JMP_BB(ibuf
, bb6
);
54 host::JGE_BB_FT(ibuf
, bb3
, bb2
);
58 a
.init(reinterpret_cast<FunctionImpl
&>(*fn
), CFGAnalysis::DEPTH_ANALYSIS
);
59 t
.check("V1/blocks", a
.getBlockCount() == 6);
60 t
.check("V1/order integrity", a
.checkIntegrity());
61 t
.check("V1/total depth", a
.getMaxBBlockDepth() == 1);
62 t
.check("V1/individual depth 1", a
.getDepth(reinterpret_cast<BasicBlockImpl
*>(bb1
._ptr
)) == 0);
63 t
.check("V1/individual depth 2", a
.getDepth(reinterpret_cast<BasicBlockImpl
*>(bb2
._ptr
)) == 0);
64 t
.check("V1/individual depth 3", a
.getDepth(reinterpret_cast<BasicBlockImpl
*>(bb3
._ptr
)) == 1);
65 t
.check("V1/individual depth 4", a
.getDepth(reinterpret_cast<BasicBlockImpl
*>(bb4
._ptr
)) == 1);
66 t
.check("V1/individual depth 5", a
.getDepth(reinterpret_cast<BasicBlockImpl
*>(bb5
._ptr
)) == 1);
67 t
.check("V1/individual depth 6", a
.getDepth(reinterpret_cast<BasicBlockImpl
*>(bb6
._ptr
)) == 1);
70 std::unique_ptr
<Function
> fn
= mi
->createFnc
<FT0_c
>(alloc
);
71 InstructionStreamBuffer
<256> ibuf
;
73 Ref
<BasicBlock
> bb1
= fn
->getStartBlock();
74 Ref
<BasicBlock
> bb2
= fn
->createBasicBlock();
75 Ref
<BasicBlock
> bb3
= fn
->createBasicBlock();
76 Ref
<BasicBlock
> bb4
= fn
->createBasicBlock();
77 Ref
<BasicBlock
> bb5
= fn
->createBasicBlock();
78 Ref
<BasicBlock
> bb6
= fn
->createBasicBlock();
81 host::JGE_BB_FT(ibuf
, bb2
, bb3
);
85 host::JMP_BB(ibuf
, bb3
);
89 host::JGE_BB_FT(ibuf
, bb4
, bb6
);
93 host::JMP_BB(ibuf
, bb5
);
97 host::JL_BB_FT(ibuf
, bb2
, bb6
);
105 a
.init(reinterpret_cast<FunctionImpl
&>(*fn
), CFGAnalysis::DEPTH_ANALYSIS
);
106 t
.check("V2/blocks", a
.getBlockCount() == 6);
107 t
.check("V2/order integrity", a
.checkIntegrity());
108 t
.check("V2/total depth", a
.getMaxBBlockDepth() == 1);
109 t
.check("V2/individual depth 1", a
.getDepth(reinterpret_cast<BasicBlockImpl
*>(bb1
._ptr
)) == 0);
110 t
.check("V2/individual depth 2", a
.getDepth(reinterpret_cast<BasicBlockImpl
*>(bb2
._ptr
)) == 1);
111 t
.check("V2/individual depth 3", a
.getDepth(reinterpret_cast<BasicBlockImpl
*>(bb3
._ptr
)) == 1);
112 t
.check("V2/individual depth 4", a
.getDepth(reinterpret_cast<BasicBlockImpl
*>(bb4
._ptr
)) == 1);
113 t
.check("V2/individual depth 5", a
.getDepth(reinterpret_cast<BasicBlockImpl
*>(bb5
._ptr
)) == 1);
114 t
.check("V2/individual depth 6", a
.getDepth(reinterpret_cast<BasicBlockImpl
*>(bb6
._ptr
)) == 0);
118 static UnitTestRun
_("CFG Analysis", test
);