Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/rellic/AST/ASTBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,11 @@ class ASTBuilder {
}
// Initializer list
clang::InitListExpr *CreateInitList(std::vector<clang::Expr *> &exprs);
// Compound literal
clang::CompoundLiteralExpr *CreateCompoundLit(clang::QualType type,
clang::Expr *expr);
// Compound statement
clang::CompoundStmt *CreateCompound(std::vector<clang::Stmt *> &stmts);
clang::CompoundStmt *CreateCompoundStmt(std::vector<clang::Stmt *> &stmts);
// If statement
clang::IfStmt *CreateIf(clang::Expr *cond, clang::Stmt *then_val,
clang::Stmt *else_val = nullptr);
Expand Down
32 changes: 32 additions & 0 deletions include/rellic/BC/Compat/DerivedTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2021-present, Trail of Bits, Inc.
* All rights reserved.
*
* This source code is licensed in accordance with the terms specified in
* the LICENSE file found in the root directory of this source tree.
*/
#pragma once

#include <gflags/gflags.h>
#include <glog/logging.h>
#include <llvm/IR/DerivedTypes.h>

#include "rellic/BC/Version.h"

namespace rellic {

unsigned GetNumElements(llvm::VectorType *type) {
#if LLVM_VERSION_NUMBER >= LLVM_VERSION(12, 0)
if (auto fixed_vec_ty = llvm::dyn_cast<llvm::FixedVectorType>(type)) {
return fixed_vec_ty->getNumElements();
}

LOG(FATAL) << "Given VectorType is not a FixedVectorType.";

return 0U;
#else
return type->getNumElements();
#endif
}

} // namespace rellic
15 changes: 13 additions & 2 deletions lib/AST/ASTBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ static unsigned GetOperatorPrecedence(clang::Expr *op) {
if (clang::isa<clang::DeclRefExpr>(op) ||
clang::isa<clang::IntegerLiteral>(op) ||
clang::isa<clang::FloatingLiteral>(op) ||
clang::isa<clang::InitListExpr>(op) ||
clang::isa<clang::CompoundLiteralExpr>(op) ||
clang::isa<clang::ParenExpr>(op)) {
return CExprPrecedence::Value;
}
Expand Down Expand Up @@ -363,7 +365,7 @@ clang::InitListExpr *ASTBuilder::CreateInitList(
return er.getAs<clang::InitListExpr>();
}

clang::CompoundStmt *ASTBuilder::CreateCompound(
clang::CompoundStmt *ASTBuilder::CreateCompoundStmt(
std::vector<clang::Stmt *> &stmts) {
// sema.ActOnStartOfCompoundStmt(/*isStmtExpr=*/false);
// auto sr{sema.ActOnCompoundStmt(clang::SourceLocation(),
Expand All @@ -372,7 +374,16 @@ clang::CompoundStmt *ASTBuilder::CreateCompound(
// sema.ActOnFinishOfCompoundStmt();
// CHECK(sr.isUsable());
// return sr.getAs<clang::CompoundStmt>();
return CreateCompoundStmt(ctx, stmts);
return rellic::CreateCompoundStmt(ctx, stmts);
}

clang::CompoundLiteralExpr *ASTBuilder::CreateCompoundLit(clang::QualType type,
clang::Expr *expr) {
auto er{sema.BuildCompoundLiteralExpr(clang::SourceLocation(),
ctx.getTrivialTypeSourceInfo(type),
clang::SourceLocation(), expr)};
CHECK(er.isUsable());
return er.getAs<clang::CompoundLiteralExpr>();
}

clang::IfStmt *ASTBuilder::CreateIf(clang::Expr *cond, clang::Stmt *then_val,
Expand Down
6 changes: 3 additions & 3 deletions lib/AST/CondBasedRefine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void CondBasedRefine::CreateIfThenElseStmts(IfStmtVec worklist) {
substitutions[stmt] = nullptr;
}
// Create our new if-then
auto sub = ast.CreateIf(lhs->getCond(), ast.CreateCompound(thens));
auto sub = ast.CreateIf(lhs->getCond(), ast.CreateCompoundStmt(thens));
// Create an else branch if possible
if (!elses.empty()) {
// Erase else statements from the AST and `worklist`
Expand All @@ -111,7 +111,7 @@ void CondBasedRefine::CreateIfThenElseStmts(IfStmtVec worklist) {
substitutions[stmt] = nullptr;
}
// Add the else branch
sub->setElse(ast.CreateCompound(elses));
sub->setElse(ast.CreateCompoundStmt(elses));
}
// Replace `lhs` with the new `sub`
substitutions[lhs] = sub;
Expand All @@ -131,7 +131,7 @@ bool CondBasedRefine::VisitCompoundStmt(clang::CompoundStmt *compound) {
new_body.push_back(stmt);
}
}
substitutions[compound] = ast.CreateCompound(new_body);
substitutions[compound] = ast.CreateCompoundStmt(new_body);
}
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/DeadStmtElim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ bool DeadStmtElim::VisitCompoundStmt(clang::CompoundStmt *compound) {
}
// Create the a new compound
if (changed || new_body.size() < compound->size()) {
substitutions[compound] = ast.CreateCompound(new_body);
substitutions[compound] = ast.CreateCompoundStmt(new_body);
}
return true;
}
Expand Down
14 changes: 7 additions & 7 deletions lib/AST/GenerateAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ StmtVec GenerateAST::CreateRegionStmts(llvm::Region *region) {
} else {
// Create a compound, wrapping the block
auto block_body = CreateBasicBlockStmts(block);
compound = ast.CreateCompound(block_body);
compound = ast.CreateCompoundStmt(block_body);
}
// Gate the compound behind a reaching condition
block_stmts[block] = ast.CreateIf(GetOrCreateReachingCond(block), compound);
Expand Down Expand Up @@ -280,7 +280,7 @@ void GenerateAST::RefineLoopSuccessors(llvm::Loop *loop, BBSet &members,
clang::CompoundStmt *GenerateAST::StructureAcyclicRegion(llvm::Region *region) {
DLOG(INFO) << "Region " << GetRegionNameStr(region) << " is acyclic";
auto region_body = CreateRegionStmts(region);
return ast.CreateCompound(region_body);
return ast.CreateCompoundStmt(region_body);
}

clang::CompoundStmt *GenerateAST::StructureCyclicRegion(llvm::Region *region) {
Expand All @@ -293,7 +293,7 @@ clang::CompoundStmt *GenerateAST::StructureCyclicRegion(llvm::Region *region) {
// a recognized natural loop. Cyclic regions may only be fragments
// of a larger loop structure.
if (!loop) {
return ast.CreateCompound(region_body);
return ast.CreateCompoundStmt(region_body);
}
// Refine loop members and successors without invalidating LoopInfo
BBSet members, successors;
Expand Down Expand Up @@ -331,17 +331,17 @@ clang::CompoundStmt *GenerateAST::StructureCyclicRegion(llvm::Region *region) {
CHECK(it != loop_body.end());
// Create a loop exiting `break` statement
StmtVec break_stmt({ast.CreateBreak()});
auto exit_stmt = ast.CreateIf(cond, ast.CreateCompound(break_stmt));
auto exit_stmt = ast.CreateIf(cond, ast.CreateCompoundStmt(break_stmt));
// Insert it after the exiting block statement
loop_body.insert(std::next(it), exit_stmt);
}
// Create the loop statement
auto loop_stmt =
ast.CreateWhile(ast.CreateTrue(), ast.CreateCompound(loop_body));
ast.CreateWhile(ast.CreateTrue(), ast.CreateCompoundStmt(loop_body));
// Insert it at the beginning of the region body
region_body.insert(region_body.begin(), loop_stmt);
// Structure the rest of the loop body as a acyclic region
return ast.CreateCompound(region_body);
return ast.CreateCompoundStmt(region_body);
}

clang::CompoundStmt *GenerateAST::StructureRegion(llvm::Region *region) {
Expand Down Expand Up @@ -439,7 +439,7 @@ bool GenerateAST::runOnModule(llvm::Module &module) {
fbody.push_back(stmt);
}
// Set body to a new compound
fdefn->setBody(ast.CreateCompound(fbody));
fdefn->setBody(ast.CreateCompoundStmt(fbody));
}

return true;
Expand Down
Loading