Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply TBAA Metadata as Suggested by the Book #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 21 additions & 8 deletions Chapter07/tinylang/lib/CodeGen/CGProcedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,17 @@ void CGProcedure::writeVariable(llvm::BasicBlock *BB,
writeLocalVariable(BB, D, Val);
else if (V->getEnclosingDecl() ==
CGM.getModuleDeclaration()) {
Builder.CreateStore(Val, CGM.getGlobal(D));
llvm::Instruction *Inst = Builder.CreateStore(Val, CGM.getGlobal(D));
CGM.decorateInst(Inst, V->getType());
} else
llvm::report_fatal_error(
"Nested procedures not yet supported");
} else if (auto *FP =
llvm::dyn_cast<FormalParameterDeclaration>(
D)) {
if (FP->isVar()) {
Builder.CreateStore(Val, FormalParams[FP]);
llvm::Instruction *Inst = Builder.CreateStore(Val, FormalParams[FP]);
CGM.decorateInst(Inst, FP->getType());
} else
writeLocalVariable(BB, D, Val);
} else
Expand All @@ -140,7 +142,9 @@ llvm::Value *CGProcedure::readVariable(llvm::BasicBlock *BB,
auto *Global = CGM.getGlobal(D);
if (!LoadVal)
return Global;
return Builder.CreateLoad(mapType(D), Global);
llvm::Instruction *Inst = Builder.CreateLoad(mapType(D), Global);
CGM.decorateInst(Inst, V->getType());
return Inst;
} else
llvm::report_fatal_error(
"Nested procedures not yet supported");
Expand All @@ -150,9 +154,11 @@ llvm::Value *CGProcedure::readVariable(llvm::BasicBlock *BB,
if (FP->isVar()) {
if (!LoadVal)
return FormalParams[FP];
return Builder.CreateLoad(
llvm::Instruction *Inst = Builder.CreateLoad(
mapType(FP)->getPointerElementType(),
FormalParams[FP]);
CGM.decorateInst(Inst, FP->getType());
return Inst;
} else
return readLocalVariable(BB, D);
} else
Expand Down Expand Up @@ -315,8 +321,10 @@ llvm::Value *CGProcedure::emitExpr(Expr *E) {
break;
}
Val = Builder.CreateInBoundsGEP(Val, IdxList);
Val = Builder.CreateLoad(
llvm::Instruction *Inst = Builder.CreateLoad(
Val->getType()->getPointerElementType(), Val);
CGM.decorateInst(Inst, Var->getType());
Val = Inst;
} else if (auto *FieldSel =
llvm::dyn_cast<FieldSelector>(*I)) {
llvm::SmallVector<llvm::Value *, 4> IdxList;
Expand All @@ -331,13 +339,17 @@ llvm::Value *CGProcedure::emitExpr(Expr *E) {
break;
}
Val = Builder.CreateInBoundsGEP(Val, IdxList);
Val = Builder.CreateLoad(
llvm::Instruction *Inst = Builder.CreateLoad(
Val->getType()->getPointerElementType(), Val);
CGM.decorateInst(Inst, Var->getType());
Val = Inst;
} else if (auto *DerefSel =
llvm::dyn_cast<DereferenceSelector>(
*I)) {
Val = Builder.CreateLoad(
llvm::Instruction *Inst = Builder.CreateLoad(
Val->getType()->getPointerElementType(), Val);
CGM.decorateInst(Inst, Var->getType());
Val = Inst;
++I;
} else {
llvm::report_fatal_error("Unsupported selector");
Expand Down Expand Up @@ -390,7 +402,8 @@ void CGProcedure::emitStmt(AssignmentStatement *Stmt) {
if (!IdxList.empty()) {
if (Base->getType()->isPointerTy()) {
Base = Builder.CreateInBoundsGEP(Base, IdxList);
Builder.CreateStore(Val, Base);
llvm::Instruction *Inst = Builder.CreateStore(Val, Base);
CGM.decorateInst(Inst, Desig->getType());
}
else {
llvm::report_fatal_error("should not happen");
Expand Down