forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRBuilder.h
237 lines (205 loc) · 8.42 KB
/
IRBuilder.h
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
//===--- IRBuilder.h - Swift IR Builder -------------------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines Swift's specialization of llvm::IRBuilder.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_IRGEN_IRBUILDER_H
#define SWIFT_IRGEN_IRBUILDER_H
#include "llvm/ADT/PointerUnion.h"
#include "llvm/IR/IRBuilder.h"
#include "swift/Basic/LLVM.h"
#include "Address.h"
#include "IRGen.h"
namespace swift {
namespace irgen {
typedef llvm::IRBuilder<> IRBuilderBase;
class IRBuilder : public IRBuilderBase {
// Without this, it keeps resolving to llvm::IRBuilderBase because
// of the injected class name.
typedef irgen::IRBuilderBase IRBuilderBase;
/// The block containing the insertion point when the insertion
/// point was last cleared. Used only for preserving block
/// ordering.
llvm::BasicBlock *ClearedIP;
public:
IRBuilder(llvm::LLVMContext &Context)
: IRBuilderBase(Context), ClearedIP(nullptr) {}
/// Determines if the current location is apparently reachable. The
/// invariant we maintain is that the insertion point of the builder
/// always points within a block unless the current location is
/// logically unreachable. All the low-level routines which emit
/// branches leave the insertion point in the original block, just
/// after the branch. High-level routines may then indicate
/// unreachability by clearing the insertion point.
bool hasValidIP() const { return GetInsertBlock() != nullptr; }
/// Determines whether we're currently inserting after a terminator.
/// This is really just there for asserts.
bool hasPostTerminatorIP() const {
return GetInsertBlock() != nullptr &&
!GetInsertBlock()->empty() &&
isa<llvm::TerminatorInst>(GetInsertBlock()->back());
}
void ClearInsertionPoint() {
assert(hasValidIP() && "clearing invalid insertion point!");
assert(ClearedIP == nullptr);
/// Whenever we clear the insertion point, remember where we were.
ClearedIP = GetInsertBlock();
IRBuilderBase::ClearInsertionPoint();
}
void SetInsertPoint(llvm::BasicBlock *BB) {
ClearedIP = nullptr;
IRBuilderBase::SetInsertPoint(BB);
}
void SetInsertPoint(llvm::BasicBlock *BB, llvm::BasicBlock::iterator before) {
ClearedIP = nullptr;
IRBuilderBase::SetInsertPoint(BB, before);
}
/// A stable insertion point in the function. "Stable" means that
/// it will point to the same location in the function, even if
/// instructions are subsequently added to the current basic block.
class StableIP {
/// Either an instruction that we're inserting after or the basic
/// block that we're inserting at the beginning of.
typedef llvm::PointerUnion<llvm::Instruction*, llvm::BasicBlock*> UnionTy;
UnionTy After;
public:
StableIP() = default;
explicit StableIP(const IRBuilder &Builder) {
if (!Builder.hasValidIP()) {
After = UnionTy();
assert(!isValid());
return;
}
llvm::BasicBlock *curBlock = Builder.GetInsertBlock();
assert(Builder.GetInsertPoint() == curBlock->end());
if (curBlock->empty())
After = curBlock;
else
After = &curBlock->back();
}
/// Does this stable IP point to a valid location?
bool isValid() const {
return !After.isNull();
}
/// Insert an unparented instruction at this insertion point.
/// Note that inserting multiple instructions at an IP will cause
/// them to end up in reverse order.
void insert(llvm::Instruction *I) {
assert(isValid() && "inserting at invalid location!");
assert(I->getParent() == nullptr);
if (llvm::BasicBlock *block = After.dyn_cast<llvm::BasicBlock*>()) {
block->getInstList().push_front(I);
} else {
llvm::Instruction *afterInsn = After.get<llvm::Instruction*>();
afterInsn->getParent()->getInstList().insertAfter(
afterInsn->getIterator(), I);
}
}
// Support for being placed in pointer unions.
void *getOpaqueValue() const { return After.getOpaqueValue(); }
static StableIP getFromOpaqueValue(void *p) {
StableIP result;
result.After = UnionTy::getFromOpaqueValue(p);
return result;
}
enum { NumLowBitsAvailable
= llvm::PointerLikeTypeTraits<UnionTy>::NumLowBitsAvailable };
};
/// Capture a stable reference to the current IP.
StableIP getStableIP() const {
return StableIP(*this);
}
llvm::LoadInst *CreateLoad(llvm::Value *addr, Alignment align,
const llvm::Twine &name = "") {
llvm::LoadInst *load = IRBuilderBase::CreateLoad(addr, name);
load->setAlignment(align.getValue());
return load;
}
llvm::LoadInst *CreateLoad(Address addr, const llvm::Twine &name = "") {
return CreateLoad(addr.getAddress(), addr.getAlignment(), name);
}
llvm::StoreInst *CreateStore(llvm::Value *value, llvm::Value *addr,
Alignment align) {
llvm::StoreInst *store = IRBuilderBase::CreateStore(value, addr);
store->setAlignment(align.getValue());
return store;
}
llvm::StoreInst *CreateStore(llvm::Value *value, Address addr) {
return CreateStore(value, addr.getAddress(), addr.getAlignment());
}
// These are deleted because we want to force the caller to specify
// an alignment.
llvm::LoadInst *CreateLoad(llvm::Value *addr,
const llvm::Twine &name = "") = delete;
llvm::StoreInst *CreateStore(llvm::Value *value, llvm::Value *addr) = delete;
using IRBuilderBase::CreateStructGEP;
Address CreateStructGEP(Address address, unsigned index, Size size,
const llvm::Twine &name = "") {
llvm::Value *addr = CreateStructGEP(
address.getType()->getElementType(), address.getAddress(),
index, name);
return Address(addr, address.getAlignment().alignmentAtOffset(size));
}
/// Given a pointer to an array element, GEP to the array element
/// N elements past it. The type is not changed.
Address CreateConstArrayGEP(Address base, unsigned index, Size eltSize,
const llvm::Twine &name = "") {
auto addr = CreateConstInBoundsGEP1_32(
base.getType()->getElementType(), base.getAddress(), index, name);
return Address(addr,
base.getAlignment().alignmentAtOffset(eltSize * index));
}
/// Given an i8*, GEP to N bytes past it.
Address CreateConstByteArrayGEP(Address base, Size offset,
const llvm::Twine &name = "") {
auto addr = CreateConstInBoundsGEP1_32(
base.getType()->getElementType(), base.getAddress(), offset.getValue(),
name);
return Address(addr, base.getAlignment().alignmentAtOffset(offset));
}
using IRBuilderBase::CreateBitCast;
Address CreateBitCast(Address address, llvm::Type *type,
const llvm::Twine &name = "") {
llvm::Value *addr = CreateBitCast(address.getAddress(), type, name);
return Address(addr, address.getAlignment());
}
/// Insert the given basic block after the IP block and move the
/// insertion point to it. Only valid if the IP is valid.
void emitBlock(llvm::BasicBlock *BB);
using IRBuilderBase::CreateMemCpy;
llvm::CallInst *CreateMemCpy(Address dest, Address src, Size size) {
return CreateMemCpy(dest.getAddress(), src.getAddress(),
size.getValue(),
std::min(dest.getAlignment(),
src.getAlignment()).getValue());
}
};
} // end namespace irgen
} // end namespace swift
namespace llvm {
template <> class PointerLikeTypeTraits<swift::irgen::IRBuilder::StableIP> {
typedef swift::irgen::IRBuilder::StableIP type;
public:
static void *getAsVoidPointer(type IP) {
return IP.getOpaqueValue();
}
static type getFromVoidPointer(void *p) {
return type::getFromOpaqueValue(p);
}
// The number of bits available are the min of the two pointer types.
enum {
NumLowBitsAvailable = type::NumLowBitsAvailable
};
};
}
#endif