Adding copyright notices to most files. Also add readme file, and some
[jitcs.git] / tests / test_cfg_analysis.cpp
blob3d9408609c300605ca07a002ea8ffe8dad8dc54b
1 //===-- tests/test_cfg_analysis.cpp -----------------------------*- C++ -*-===//
2 // Test of the loop depth detection.
3 //
4 // Copyright (C) 2013-2014 Dirk Steinke.
5 // See copyright and license notice in COPYRIGHT or include/jitcs.h
6 //===----------------------------------------------------------------------===//
8 #include "jitcs.h"
9 #include "unittest.h"
10 #include "jitcs_instructionstream.h"
11 #include "jitcs_dumper.h"
12 #include "jitcs_int_cfg_analysis.h"
14 #include <stdio.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();
33 ibuf.start(bb1);
34 host::JGE_BB_FT(ibuf, bb2, bb3);
35 ibuf.end();
37 ibuf.start(bb2);
38 host::RET(ibuf);
39 ibuf.end();
41 ibuf.start(bb3);
42 host::JGE_BB_FT(ibuf, bb4, bb5);
43 ibuf.end();
45 ibuf.start(bb4);
46 host::JMP_BB(ibuf, bb6);
47 ibuf.end();
49 ibuf.start(bb5);
50 host::JMP_BB(ibuf, bb6);
51 ibuf.end();
53 ibuf.start(bb6);
54 host::JGE_BB_FT(ibuf, bb3, bb2);
55 ibuf.end();
57 CFGAnalysis a;
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();
80 ibuf.start(bb1);
81 host::JGE_BB_FT(ibuf, bb2, bb3);
82 ibuf.end();
84 ibuf.start(bb2);
85 host::JMP_BB(ibuf, bb3);
86 ibuf.end();
88 ibuf.start(bb3);
89 host::JGE_BB_FT(ibuf, bb4, bb6);
90 ibuf.end();
92 ibuf.start(bb4);
93 host::JMP_BB(ibuf, bb5);
94 ibuf.end();
96 ibuf.start(bb5);
97 host::JL_BB_FT(ibuf, bb2, bb6);
98 ibuf.end();
100 ibuf.start(bb6);
101 host::RET(ibuf);
102 ibuf.end();
104 CFGAnalysis a;
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);