blockPredication_test.cpp 4.97 KB
Newer Older
guruhegde's avatar
guruhegde committed
1 2 3 4
#include "catch.hpp"

#include <llvm/ADT/SmallVector.h>
#include <llvm/ADT/SmallPtrSet.h>
guruhegde's avatar
guruhegde committed
5 6 7
#include <llvm/Analysis/DominanceFrontier.h>
#include <llvm/Analysis/PostDominators.h>
#include <llvm/Analysis/RegionInfo.h>
guruhegde's avatar
guruhegde committed
8 9
#include <llvm/IR/Function.h>
#include <llvm/IR/Module.h>
guruhegde's avatar
guruhegde committed
10
#include <llvm/IR/CFG.h>
guruhegde's avatar
guruhegde committed
11 12 13 14 15 16 17 18 19 20 21
#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/SourceMgr.h>
#include "BlockPredication.h"
#include "PacketPathAnalysis.h"
#include "ToolUtil.h"
#include "Util.h"
#include <memory.h>

extern std::string input_dir; // This contains path to input test files

using namespace llvm;
guruhegde's avatar
guruhegde committed
22
using namespace std;
guruhegde's avatar
guruhegde committed
23 24
using namespace tas;

guruhegde's avatar
guruhegde committed
25 26 27 28 29 30 31 32 33 34
static LLVMContext C;
static SMDiagnostic Err; 

static unique_ptr<Module> parseIR(string Filename, string FileDir) {
  std::unique_ptr<Module> M (parseIRFile(FileDir + Filename,  Err, C));
  if (!M)
    Err.print("Error parsing IR: ", errs());
  return M;
}

guruhegde's avatar
guruhegde committed
35 36 37
TEST_CASE("simple if else") {
  std::string fileprefix = "blockpredication_test1";
  auto M = parseIR(generateIR(fileprefix + string(".c"), input_dir), input_dir);
guruhegde's avatar
guruhegde committed
38
  REQUIRE(M != nullptr);
guruhegde's avatar
guruhegde committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  auto F = M->getFunction("fn");
  BlockPredication BP(F);
  BP.run();

  // For debug purpose
  auto asmFile = writeToAsmFile(*M);

  // Generate object for unit under test.
  auto TestObject = generateObject(writeToBitCodeFile(*M));
  auto binary = linkObjects(vector<string>{TestObject}, fileprefix);

  // Run the binary
  binary.insert(0, "./");
  auto ret = system(binary.c_str());
  REQUIRE(ret == 0);
guruhegde's avatar
guruhegde committed
54 55
}

guruhegde's avatar
guruhegde committed
56 57 58
TEST_CASE("multiiple if else blocks") {
  std::string fileprefix = "blockpredication_test2";
  auto M = parseIR(generateIR(fileprefix + string(".c"), input_dir), input_dir);
guruhegde's avatar
guruhegde committed
59
  REQUIRE(M != nullptr);
guruhegde's avatar
guruhegde committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  auto F = M->getFunction("fn");
  BlockPredication BP(F);
  BP.run();

  // For debug purpose
  auto asmFile = writeToAsmFile(*M);

  // Generate object for unit under test.
  auto TestObject = generateObject(writeToBitCodeFile(*M));
  auto binary = linkObjects(vector<string>{TestObject}, fileprefix);

  // Run the binary
  binary.insert(0, "./");
  auto ret = system(binary.c_str());
  REQUIRE(ret == 0);
guruhegde's avatar
guruhegde committed
75 76
}

guruhegde's avatar
guruhegde committed
77
TEST_CASE("single goto") {
78 79
  std::string fileprefix = "blockpredication_test3";
  auto M = parseIR(generateIR(fileprefix + string(".c"), input_dir), input_dir);
guruhegde's avatar
guruhegde committed
80
  REQUIRE(M != nullptr);
guruhegde's avatar
guruhegde committed
81
  auto F = M->getFunction("fn");
guruhegde's avatar
guruhegde committed
82 83
  BlockPredication BP(F);
  BP.run();
84 85 86 87 88 89 90 91 92 93 94 95

  auto asmFile = writeToAsmFile(*M);

  // Generate object for unit under test.
  auto TestObject = generateObject(writeToBitCodeFile(*M));

  auto binary = linkObjects(vector<string>{TestObject}, fileprefix);

  // Run the binary
  binary.insert(0, "./");
  auto ret = system(binary.c_str());
  REQUIRE(ret == 0);
guruhegde's avatar
guruhegde committed
96 97 98
}

TEST_CASE("predicated block execution, multiple goto") {
99 100
  std::string fileprefix = "blockpredication_test4";
  auto M = parseIR(generateIR(fileprefix + string(".c"), input_dir), input_dir);
guruhegde's avatar
guruhegde committed
101
  REQUIRE(M != nullptr);
guruhegde's avatar
guruhegde committed
102
  auto F = M->getFunction("fn");
guruhegde's avatar
guruhegde committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

  BlockPredication BP(F);
  BP.run();

  auto asmFile = writeToAsmFile(*M);

  // Generate object for unit under test.
  auto TestObject = generateObject(writeToBitCodeFile(*M));

  auto binary = linkObjects(vector<string>{TestObject}, fileprefix);

  // Run the binary
  binary.insert(0, "./");
  auto ret = system(binary.c_str());
  REQUIRE(ret == 0);
}

TEST_CASE("predicated block execution, multiple ifelse") {
  std::string fileprefix = "blockpredication_test5";
  auto M = parseIR(generateIR(fileprefix + string(".c"), input_dir), input_dir);
  REQUIRE(M != nullptr);
  auto F = M->getFunction("multi_conditionals_fn");

guruhegde's avatar
guruhegde committed
126 127
  BlockPredication BP(F);
  BP.run();
128 129 130 131 132 133 134 135 136 137 138 139

  auto asmFile = writeToAsmFile(*M);

  // Generate object for unit under test.
  auto TestObject = generateObject(writeToBitCodeFile(*M));

  auto binary = linkObjects(vector<string>{TestObject}, fileprefix);

  // Run the binary
  binary.insert(0, "./");
  auto ret = system(binary.c_str());
  REQUIRE(ret == 0);
guruhegde's avatar
guruhegde committed
140
}
guruhegde's avatar
guruhegde committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

TEST_CASE("lot of if else") {
  std::string fileprefix = "blockpredication_test6";
  auto M = parseIR(generateIR(fileprefix + string(".c"), input_dir), input_dir);
  REQUIRE(M != nullptr);
  auto F = M->getFunction("fn");

  BlockPredication BP(F);
  BP.run();

  auto asmFile = writeToAsmFile(*M);

  // Generate object for unit under test.
  auto TestObject = generateObject(writeToBitCodeFile(*M));

  auto binary = linkObjects(vector<string>{TestObject}, fileprefix);

  // Run the binary
  binary.insert(0, "./");
  auto ret = system(binary.c_str());
  REQUIRE(ret == 0);
}
guruhegde's avatar
guruhegde committed
163 164 165 166 167 168 169 170 171 172

TEST_CASE("fast_flows_packet") {
  std::string fileprefix = "fast_flows";
  auto M = parseIR(generateIR(fileprefix + string(".c"), input_dir, true), input_dir);
  REQUIRE(M != nullptr);
  auto F = M->getFunction("fast_flows_packet");

  BlockPredication BP(F);
  BP.run();

guruhegde's avatar
guruhegde committed
173
  auto asmFile = writeToAsmFile(*M);
guruhegde's avatar
guruhegde committed
174 175 176 177 178 179 180 181 182 183 184

  // Generate object for unit under test.
  auto TestObject = generateObject(writeToBitCodeFile(*M));

  //auto binary = linkObjects(vector<string>{TestObject}, fileprefix);

  // Run the binary
  //binary.insert(0, "./");
  //auto ret = system(binary.c_str());
  //REQUIRE(ret == 0);
}