The check to see if an item was already on the heap was randomly hitting.
[jitcs.git] / tests / test_cfg_analysis.cpp
blobe0249215477105191b8c0a8a2754281bf657a405
1 #include "jitcs.h"
2 #include "unittest.h"
3 #include "jitcs_instructionstream.h"
4 #include "jitcs_dumper.h"
5 #include "jitcs_int_cfg_analysis.h"
7 #include <stdio.h>
9 using namespace jitcs;
11 static void test(UnitTest& t) {
12 typedef void (__cdecl *FT0_c)();
13 RefCounter<IMachineInfo> mi = host::GetMachineInfo();
14 RefCounter<TempAllocator> alloc(new TempAllocator);
16 std::unique_ptr<Function> fn = mi->createFnc<FT0_c>(alloc);
17 InstructionStreamBuffer<256> ibuf;
19 Ref<BasicBlock> bb1 = fn->getStartBlock();
20 Ref<BasicBlock> bb2 = fn->createBasicBlock();
21 Ref<BasicBlock> bb3 = fn->createBasicBlock();
22 Ref<BasicBlock> bb4 = fn->createBasicBlock();
23 Ref<BasicBlock> bb5 = fn->createBasicBlock();
24 Ref<BasicBlock> bb6 = fn->createBasicBlock();
26 ibuf.start(bb1);
27 host::JGE_BB_FT(ibuf, bb2, bb3);
28 ibuf.end();
30 ibuf.start(bb2);
31 host::RET(ibuf);
32 ibuf.end();
34 ibuf.start(bb3);
35 host::JGE_BB_FT(ibuf, bb4, bb5);
36 ibuf.end();
38 ibuf.start(bb4);
39 host::JMP_BB(ibuf, bb6);
40 ibuf.end();
42 ibuf.start(bb5);
43 host::JMP_BB(ibuf, bb6);
44 ibuf.end();
46 ibuf.start(bb6);
47 host::JGE_BB_FT(ibuf, bb3, bb2);
48 ibuf.end();
50 CFGAnalysis a;
51 a.init(reinterpret_cast<FunctionImpl&>(*fn), CFGAnalysis::DEPTH_ANALYSIS);
52 t.check("V1/blocks", a.getBlockCount() == 6);
53 t.check("V1/order integrity", a.checkIntegrity());
54 t.check("V1/total depth", a.getMaxBBlockDepth() == 1);
55 t.check("V1/individual depth 1", a.getDepth(reinterpret_cast<BasicBlockImpl*>(bb1._ptr)) == 0);
56 t.check("V1/individual depth 2", a.getDepth(reinterpret_cast<BasicBlockImpl*>(bb2._ptr)) == 0);
57 t.check("V1/individual depth 3", a.getDepth(reinterpret_cast<BasicBlockImpl*>(bb3._ptr)) == 1);
58 t.check("V1/individual depth 4", a.getDepth(reinterpret_cast<BasicBlockImpl*>(bb4._ptr)) == 1);
59 t.check("V1/individual depth 5", a.getDepth(reinterpret_cast<BasicBlockImpl*>(bb5._ptr)) == 1);
60 t.check("V1/individual depth 6", a.getDepth(reinterpret_cast<BasicBlockImpl*>(bb6._ptr)) == 1);
63 std::unique_ptr<Function> fn = mi->createFnc<FT0_c>(alloc);
64 InstructionStreamBuffer<256> ibuf;
66 Ref<BasicBlock> bb1 = fn->getStartBlock();
67 Ref<BasicBlock> bb2 = fn->createBasicBlock();
68 Ref<BasicBlock> bb3 = fn->createBasicBlock();
69 Ref<BasicBlock> bb4 = fn->createBasicBlock();
70 Ref<BasicBlock> bb5 = fn->createBasicBlock();
71 Ref<BasicBlock> bb6 = fn->createBasicBlock();
73 ibuf.start(bb1);
74 host::JGE_BB_FT(ibuf, bb2, bb3);
75 ibuf.end();
77 ibuf.start(bb2);
78 host::JMP_BB(ibuf, bb3);
79 ibuf.end();
81 ibuf.start(bb3);
82 host::JGE_BB_FT(ibuf, bb4, bb6);
83 ibuf.end();
85 ibuf.start(bb4);
86 host::JMP_BB(ibuf, bb5);
87 ibuf.end();
89 ibuf.start(bb5);
90 host::JL_BB_FT(ibuf, bb2, bb6);
91 ibuf.end();
93 ibuf.start(bb6);
94 host::RET(ibuf);
95 ibuf.end();
97 CFGAnalysis a;
98 a.init(reinterpret_cast<FunctionImpl&>(*fn), CFGAnalysis::DEPTH_ANALYSIS);
99 t.check("V2/blocks", a.getBlockCount() == 6);
100 t.check("V2/order integrity", a.checkIntegrity());
101 t.check("V2/total depth", a.getMaxBBlockDepth() == 1);
102 t.check("V2/individual depth 1", a.getDepth(reinterpret_cast<BasicBlockImpl*>(bb1._ptr)) == 0);
103 t.check("V2/individual depth 2", a.getDepth(reinterpret_cast<BasicBlockImpl*>(bb2._ptr)) == 1);
104 t.check("V2/individual depth 3", a.getDepth(reinterpret_cast<BasicBlockImpl*>(bb3._ptr)) == 1);
105 t.check("V2/individual depth 4", a.getDepth(reinterpret_cast<BasicBlockImpl*>(bb4._ptr)) == 1);
106 t.check("V2/individual depth 5", a.getDepth(reinterpret_cast<BasicBlockImpl*>(bb5._ptr)) == 1);
107 t.check("V2/individual depth 6", a.getDepth(reinterpret_cast<BasicBlockImpl*>(bb6._ptr)) == 0);
111 static UnitTestRun _("CFG Analysis", test);