1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
//===-- irstate.cpp -------------------------------------------------------===//
//
// LDC – the LLVM D compiler
//
// This file is distributed under the BSD-style LDC license. See the LICENSE
// file for details.
//
//===----------------------------------------------------------------------===//
#include "gen/irstate.h"
#include "dmd/declaration.h"
#include "dmd/expression.h"
#include "dmd/identifier.h"
#include "dmd/mtype.h"
#include "dmd/statement.h"
#include "gen/funcgenstate.h"
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/tollvm.h"
#include "ir/irfunction.h"
#include "llvm/IR/InlineAsm.h"
#include <cstdarg>
IRState *gIR = nullptr;
llvm::TargetMachine *gTargetMachine = nullptr;
const llvm::DataLayout *gDataLayout = nullptr;
TargetABI *gABI = nullptr;
////////////////////////////////////////////////////////////////////////////////
IRState::IRState(const char *name, llvm::LLVMContext &context)
: builder(context), module(name, context), objc(module), DBuilder(this) {
ir.state = this;
mem.addRange(&inlineAsmLocs, sizeof(inlineAsmLocs));
}
IRState::~IRState() { mem.removeRange(&inlineAsmLocs); }
FuncGenState &IRState::funcGen() {
assert(!funcGenStates.empty() && "Function stack is empty!");
return *funcGenStates.back();
}
IrFunction *IRState::func() { return &funcGen().irFunc; }
llvm::Function *IRState::topfunc() { return func()->getLLVMFunc(); }
llvm::Instruction *IRState::topallocapoint() { return funcGen().allocapoint; }
std::unique_ptr<IRBuilderScope> IRState::setInsertPoint(llvm::BasicBlock *bb) {
auto savedScope = llvm::make_unique<IRBuilderScope>(builder);
builder.SetInsertPoint(bb);
return savedScope;
}
std::unique_ptr<llvm::IRBuilderBase::InsertPointGuard>
IRState::saveInsertPoint() {
return llvm::make_unique<llvm::IRBuilderBase::InsertPointGuard>(builder);
}
bool IRState::scopereturned() {
auto bb = scopebb();
return !bb->empty() && bb->back().isTerminator();
}
llvm::BasicBlock *IRState::insertBBBefore(llvm::BasicBlock *successor,
const llvm::Twine &name) {
return llvm::BasicBlock::Create(context(), name, topfunc(), successor);
}
llvm::BasicBlock *IRState::insertBBAfter(llvm::BasicBlock *predecessor,
const llvm::Twine &name) {
auto bb = llvm::BasicBlock::Create(context(), name, topfunc());
bb->moveAfter(predecessor);
return bb;
}
llvm::BasicBlock *IRState::insertBB(const llvm::Twine &name) {
return insertBBAfter(scopebb(), name);
}
llvm::Instruction *IRState::CreateCallOrInvoke(LLFunction *Callee,
const char *Name) {
return CreateCallOrInvoke(Callee, {}, Name);
}
llvm::Instruction *IRState::CreateCallOrInvoke(LLFunction *Callee,
llvm::ArrayRef<LLValue *> Args,
const char *Name,
bool isNothrow) {
return funcGen().callOrInvoke(Callee, Callee->getFunctionType(), Args, Name,
isNothrow);
}
llvm::Instruction *IRState::CreateCallOrInvoke(LLFunction *Callee,
LLValue *Arg1,
const char *Name) {
return CreateCallOrInvoke(Callee, llvm::ArrayRef<LLValue *>(Arg1), Name);
}
llvm::Instruction *IRState::CreateCallOrInvoke(LLFunction *Callee,
LLValue *Arg1, LLValue *Arg2,
const char *Name) {
return CreateCallOrInvoke(Callee, {Arg1, Arg2}, Name);
}
llvm::Instruction *IRState::CreateCallOrInvoke(LLFunction *Callee,
LLValue *Arg1, LLValue *Arg2,
LLValue *Arg3,
const char *Name) {
return CreateCallOrInvoke(Callee, {Arg1, Arg2, Arg3}, Name);
}
llvm::Instruction *IRState::CreateCallOrInvoke(LLFunction *Callee,
LLValue *Arg1, LLValue *Arg2,
LLValue *Arg3, LLValue *Arg4,
const char *Name) {
return CreateCallOrInvoke(Callee, {Arg1, Arg2, Arg3, Arg4}, Name);
}
bool IRState::emitArrayBoundsChecks() {
if (global.params.useArrayBounds != CHECKENABLEsafeonly) {
return global.params.useArrayBounds == CHECKENABLEon;
}
// Safe functions only.
if (funcGenStates.empty()) {
return false;
}
Type *t = func()->decl->type;
return t->ty == TY::Tfunction && ((TypeFunction *)t)->trust == TRUST::safe;
}
LLConstant *
IRState::setGlobalVarInitializer(LLGlobalVariable *&globalVar,
LLConstant *initializer,
Dsymbol *symbolForLinkageAndVisibility) {
if (initializer->getType() == globalVar->getType()->getContainedType(0)) {
defineGlobal(globalVar, initializer, symbolForLinkageAndVisibility);
return globalVar;
}
// Create the global helper variable matching the initializer type.
// It inherits most properties from the existing globalVar.
auto globalHelperVar = new LLGlobalVariable(
module, initializer->getType(), globalVar->isConstant(),
globalVar->getLinkage(), nullptr, "", nullptr,
globalVar->getThreadLocalMode());
globalHelperVar->setAlignment(LLMaybeAlign(globalVar->getAlignment()));
globalHelperVar->setComdat(globalVar->getComdat());
globalHelperVar->setDLLStorageClass(globalVar->getDLLStorageClass());
globalHelperVar->setSection(globalVar->getSection());
globalHelperVar->takeName(globalVar);
defineGlobal(globalHelperVar, initializer, symbolForLinkageAndVisibility);
// Replace all existing uses of globalVar by the bitcast pointer.
auto castHelperVar = DtoBitCast(globalHelperVar, globalVar->getType());
globalVar->replaceAllUsesWith(castHelperVar);
// Register replacement for later occurrences of the original globalVar.
globalsToReplace.emplace_back(globalVar, castHelperVar);
// Reset globalVar to the helper variable.
globalVar = globalHelperVar;
return castHelperVar;
}
void IRState::replaceGlobals() {
for (const auto &pair : globalsToReplace) {
pair.first->replaceAllUsesWith(pair.second);
pair.first->eraseFromParent();
}
globalsToReplace.resize(0);
}
////////////////////////////////////////////////////////////////////////////////
LLConstant *IRState::getStructLiteralConstant(StructLiteralExp *sle) const {
return static_cast<LLConstant *>(structLiteralConstants.lookup(sle->origin));
}
void IRState::setStructLiteralConstant(StructLiteralExp *sle,
LLConstant *constant) {
structLiteralConstants[sle->origin] = constant;
}
////////////////////////////////////////////////////////////////////////////////
namespace {
template <typename F>
LLGlobalVariable *
getCachedStringLiteralImpl(llvm::Module &module,
llvm::StringMap<LLGlobalVariable *> &cache,
llvm::StringRef key, F initFactory) {
auto iter = cache.find(key);
if (iter != cache.end()) {
return iter->second;
}
LLConstant *constant = initFactory();
auto gvar =
new LLGlobalVariable(module, constant->getType(), true,
LLGlobalValue::PrivateLinkage, constant, ".str");
gvar->setUnnamedAddr(LLGlobalValue::UnnamedAddr::Global);
cache[key] = gvar;
return gvar;
}
}
LLGlobalVariable *IRState::getCachedStringLiteral(StringExp *se) {
llvm::StringMap<LLGlobalVariable *> *cache;
switch (se->sz) {
default:
llvm_unreachable("Unknown char type");
case 1:
cache = &cachedStringLiterals;
break;
case 2:
cache = &cachedWstringLiterals;
break;
case 4:
cache = &cachedDstringLiterals;
break;
}
const DArray<const unsigned char> keyData = se->peekData();
const llvm::StringRef key(reinterpret_cast<const char *>(keyData.ptr),
keyData.length);
return getCachedStringLiteralImpl(module, *cache, key, [se]() {
return buildStringLiteralConstant(se, true);
});
}
LLGlobalVariable *IRState::getCachedStringLiteral(llvm::StringRef s) {
return getCachedStringLiteralImpl(module, cachedStringLiterals, s, [&]() {
return llvm::ConstantDataArray::getString(context(), s, true);
});
}
////////////////////////////////////////////////////////////////////////////////
void IRState::addLinkerOption(llvm::ArrayRef<llvm::StringRef> options) {
llvm::SmallVector<llvm::Metadata *, 2> mdStrings;
mdStrings.reserve(options.size());
for (const auto &s : options)
mdStrings.push_back(llvm::MDString::get(context(), s));
linkerOptions.push_back(llvm::MDNode::get(context(), mdStrings));
}
void IRState::addLinkerDependentLib(llvm::StringRef libraryName) {
auto n = llvm::MDString::get(context(), libraryName);
linkerDependentLibs.push_back(llvm::MDNode::get(context(), n));
}
////////////////////////////////////////////////////////////////////////////////
llvm::CallInst *
IRState::createInlineAsmCall(const Loc &loc, llvm::InlineAsm *ia,
llvm::ArrayRef<llvm::Value *> args) {
llvm::CallInst *call = ir->CreateCall(ia, args);
addInlineAsmSrcLoc(loc, call);
#if LDC_LLVM_VER >= 1400
// a non-indirect output constraint (=> return value of call) shifts the
// constraint/argument index mapping
ptrdiff_t i = call->getType()->isVoidTy() ? 0 : -1;
for (const auto &constraintInfo : ia->ParseConstraints()) {
if (constraintInfo.isIndirect) {
call->addParamAttr(i, llvm::Attribute::get(context(),
llvm::Attribute::ElementType,
getPointeeType(args[i])));
}
++i;
}
#endif
return call;
}
void IRState::addInlineAsmSrcLoc(const Loc &loc,
llvm::CallInst *inlineAsmCall) {
// Simply use a stack of Loc* per IR module, and use index+1 as 32-bit
// cookie to be mapped back by the InlineAsmDiagnosticHandler.
// 0 is not a valid cookie.
inlineAsmLocs.push_back(loc);
auto srcLocCookie = static_cast<unsigned>(inlineAsmLocs.size());
auto constant =
LLConstantInt::get(LLType::getInt32Ty(context()), srcLocCookie);
inlineAsmCall->setMetadata(
"srcloc",
llvm::MDNode::get(context(), llvm::ConstantAsMetadata::get(constant)));
}
const Loc &IRState::getInlineAsmSrcLoc(unsigned srcLocCookie) const {
assert(srcLocCookie > 0 && srcLocCookie <= inlineAsmLocs.size());
return inlineAsmLocs[srcLocCookie - 1];
}
////////////////////////////////////////////////////////////////////////////////
IRBuilder<> *IRBuilderHelper::operator->() {
IRBuilder<> &b = state->builder;
assert(b.GetInsertBlock());
return &b;
}
////////////////////////////////////////////////////////////////////////////////
bool useMSVCEH() {
return global.params.targetTriple->isWindowsMSVCEnvironment();
}
|