Skip to content

Commit

Permalink
[clang][CGBuiltin] Refactor __readdr
Browse files Browse the repository at this point in the history
  • Loading branch information
gmh5225 authored Jun 22, 2024
1 parent 99fcd98 commit 905eee2
Showing 1 changed file with 23 additions and 105 deletions.
128 changes: 23 additions & 105 deletions clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16478,123 +16478,41 @@ Value *CodeGenFunction::EmitX86BuiltinExpr(unsigned BuiltinID,
return CI;
}
case X86::BI__readdr: {
BasicBlock *BB0 = createBasicBlock("BB0", this->CurFn);
BasicBlock *BB1 = createBasicBlock("BB1", this->CurFn);
BasicBlock *BB2 = createBasicBlock("BB2", this->CurFn);
BasicBlock *BB3 = createBasicBlock("BB3", this->CurFn);
BasicBlock *BB4 = createBasicBlock("BB4", this->CurFn);
BasicBlock *BB5 = createBasicBlock("BB5", this->CurFn);
BasicBlock *BB6 = createBasicBlock("BB6", this->CurFn);
BasicBlock *BB7 = createBasicBlock("BB7", this->CurFn);
BasicBlock *BBs[8];
for (int i = 0; i < 8; ++i) {
BBs[i] = createBasicBlock("BB" + std::to_string(i), this->CurFn);
}
BasicBlock *BBDest = createBasicBlock("BBDest", this->CurFn);
llvm::AllocaInst *AI = Builder.CreateAlloca(SizeTy);
CharUnits CU = SizeTy->getBitWidth() == 32 ? CharUnits::fromQuantity(4)
: CharUnits::fromQuantity(8);
llvm::SwitchInst *SI = Builder.CreateSwitch(Ops[0], BBDest, 8);
SI->addCase(Builder.getInt32(0), BB0);
SI->addCase(Builder.getInt32(1), BB1);
SI->addCase(Builder.getInt32(2), BB2);
SI->addCase(Builder.getInt32(3), BB3);
SI->addCase(Builder.getInt32(4), BB4);
SI->addCase(Builder.getInt32(5), BB5);
SI->addCase(Builder.getInt32(6), BB6);
SI->addCase(Builder.getInt32(7), BB7);
Builder.SetInsertPoint(BB0);
{
llvm::StringRef AsmStr =
SizeTy->getBitWidth() == 32 ? "mov %dr0, $0" : "movq %dr0, ${0:q}";
llvm::FunctionType *FTy = llvm::FunctionType::get(SizeTy, false);
llvm::InlineAsm *IA =
llvm::InlineAsm::get(FTy, AsmStr, "=r,~{dirflag},~{fpsr},~{flags}",
/*hasSideEffects=*/true);
llvm::CallInst *CI = Builder.CreateCall(IA);
Builder.CreateStore(CI, Address(AI, AI->getType(), CU));
Builder.CreateBr(BBDest);
}
Builder.SetInsertPoint(BB1);
{
llvm::StringRef AsmStr =
SizeTy->getBitWidth() == 32 ? "mov %dr1, $0" : "movq %dr1, ${0:q}";
llvm::FunctionType *FTy = llvm::FunctionType::get(SizeTy, false);
llvm::InlineAsm *IA =
llvm::InlineAsm::get(FTy, AsmStr, "=r,~{dirflag},~{fpsr},~{flags}",
/*hasSideEffects=*/true);
llvm::CallInst *CI = Builder.CreateCall(IA);
Builder.CreateStore(CI, Address(AI, AI->getType(), CU));
Builder.CreateBr(BBDest);
}
Builder.SetInsertPoint(BB2);
{
llvm::StringRef AsmStr =
SizeTy->getBitWidth() == 32 ? "mov %dr2, $0" : "movq %dr2, ${0:q}";
llvm::FunctionType *FTy = llvm::FunctionType::get(SizeTy, false);
llvm::InlineAsm *IA =
llvm::InlineAsm::get(FTy, AsmStr, "=r,~{dirflag},~{fpsr},~{flags}",
/*hasSideEffects=*/true);
llvm::CallInst *CI = Builder.CreateCall(IA);
Builder.CreateStore(CI, Address(AI, AI->getType(), CU));
Builder.CreateBr(BBDest);
}
Builder.SetInsertPoint(BB3);
{
llvm::StringRef AsmStr =
SizeTy->getBitWidth() == 32 ? "mov %dr3, $0" : "movq %dr3, ${0:q}";
llvm::FunctionType *FTy = llvm::FunctionType::get(SizeTy, false);
llvm::InlineAsm *IA =
llvm::InlineAsm::get(FTy, AsmStr, "=r,~{dirflag},~{fpsr},~{flags}",
/*hasSideEffects=*/true);
llvm::CallInst *CI = Builder.CreateCall(IA);
Builder.CreateStore(CI, Address(AI, AI->getType(), CU));
Builder.CreateBr(BBDest);
for (int i = 0; i < 8; ++i) {
SI->addCase(Builder.getInt32(i), BBs[i]);
}
Builder.SetInsertPoint(BB4);
{
llvm::StringRef AsmStr =
SizeTy->getBitWidth() == 32 ? "mov %dr4, $0" : "movq %dr4, ${0:q}";
llvm::FunctionType *FTy = llvm::FunctionType::get(SizeTy, false);
llvm::InlineAsm *IA =
llvm::InlineAsm::get(FTy, AsmStr, "=r,~{dirflag},~{fpsr},~{flags}",
/*hasSideEffects=*/true);
llvm::CallInst *CI = Builder.CreateCall(IA);
Builder.CreateStore(CI, Address(AI, AI->getType(), CU));
Builder.CreateBr(BBDest);
}
Builder.SetInsertPoint(BB5);
{
llvm::StringRef AsmStr =
SizeTy->getBitWidth() == 32 ? "mov %dr5, $0" : "movq %dr5, ${0:q}";
llvm::FunctionType *FTy = llvm::FunctionType::get(SizeTy, false);
llvm::InlineAsm *IA =
llvm::InlineAsm::get(FTy, AsmStr, "=r,~{dirflag},~{fpsr},~{flags}",
/*hasSideEffects=*/true);
llvm::CallInst *CI = Builder.CreateCall(IA);
Builder.CreateStore(CI, Address(AI, AI->getType(), CU));
Builder.CreateBr(BBDest);
}
Builder.SetInsertPoint(BB6);
{
llvm::StringRef AsmStr =
SizeTy->getBitWidth() == 32 ? "mov %dr6, $0" : "movq %dr6, ${0:q}";
llvm::FunctionType *FTy = llvm::FunctionType::get(SizeTy, false);
llvm::InlineAsm *IA =
llvm::InlineAsm::get(FTy, AsmStr, "=r,~{dirflag},~{fpsr},~{flags}",
/*hasSideEffects=*/true);
llvm::CallInst *CI = Builder.CreateCall(IA);
Builder.CreateStore(CI, Address(AI, AI->getType(), CU));
Builder.CreateBr(BBDest);

std::vector<std::string> AsmStrs;
for (int i = 0; i < 8; ++i) {
AsmStrs.push_back(SizeTy->getBitWidth() == 32
? "mov %dr" + std::to_string(i) + ", $0"
: "movq %dr" + std::to_string(i) + ", ${0:q}");
}
Builder.SetInsertPoint(BB7);
{
llvm::StringRef AsmStr =
SizeTy->getBitWidth() == 32 ? "mov %dr7, $0" : "movq %dr7, ${0:q}";

auto createInlineAsmAndBranch = [&](int regNum) {
llvm::StringRef AsmStr = AsmStrs[regNum];
llvm::FunctionType *FTy = llvm::FunctionType::get(SizeTy, false);
llvm::InlineAsm *IA =
llvm::InlineAsm::get(FTy, AsmStr, "=r,~{dirflag},~{fpsr},~{flags}",
/*hasSideEffects=*/true);
llvm::InlineAsm *IA = llvm::InlineAsm::get(
FTy, AsmStr, "=r,~{dirflag},~{fpsr},~{flags}", true);
llvm::CallInst *CI = Builder.CreateCall(IA);
Builder.CreateStore(CI, Address(AI, AI->getType(), CU));
Builder.CreateBr(BBDest);
};

for (int i = 0; i < 8; ++i) {
Builder.SetInsertPoint(BBs[i]);
createInlineAsmAndBranch(i);
}

Builder.SetInsertPoint(BBDest);
return Builder.CreateLoad(Address(AI, AI->getType(), CU));
}
Expand Down

0 comments on commit 905eee2

Please sign in to comment.