~louis/ubuntu/trusty/clamav/lp799623_fix_logrotate

« back to all changes in this revision

Viewing changes to libclamav/c++/llvm/include/llvm/Support/IRBuilder.h

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-03-12 11:30:04 UTC
  • mfrom: (0.41.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100312113004-b0fop4bkycszdd0z
Tags: 0.96~rc1+dfsg-0ubuntu1
* New upstream RC - FFE (LP: #537636):
  - Add OfficialDatabaseOnly option to clamav-base.postinst.in
  - Add LocalSocketGroup option to clamav-base.postinst.in
  - Add LocalSocketMode option to clamav-base.postinst.in
  - Add CrossFilesystems option to clamav-base.postinst.in
  - Add ClamukoScannerCount option to clamav-base.postinst.in
  - Add BytecodeSecurity opiton to clamav-base.postinst.in
  - Add DetectionStatsHostID option to clamav-freshclam.postinst.in
  - Add Bytecode option to clamav-freshclam.postinst.in
  - Add MilterSocketGroup option to clamav-milter.postinst.in
  - Add MilterSocketMode option to clamav-milter.postinst.in
  - Add ReportHostname option to clamav-milter.postinst.in
  - Bump libclamav SO version to 6.1.0 in libclamav6.install
  - Drop clamdmon from clamav.examples (no longer shipped by upstream)
  - Drop libclamav.a from libclamav-dev.install (not built by upstream)
  - Update SO version for lintian override for libclamav6
  - Add new Bytecode Testing Tool, usr/bin/clambc, to clamav.install
  - Add build-depends on python and python-setuptools for new test suite
  - Update debian/copyright for the embedded copy of llvm (using the system
    llvm is not currently feasible)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===---- llvm/Support/IRBuilder.h - Builder for LLVM Instrs ----*- C++ -*-===//
 
2
//
 
3
//                     The LLVM Compiler Infrastructure
 
4
//
 
5
// This file is distributed under the University of Illinois Open Source
 
6
// License. See LICENSE.TXT for details.
 
7
//
 
8
//===----------------------------------------------------------------------===//
 
9
//
 
10
// This file defines the IRBuilder class, which is used as a convenient way
 
11
// to create LLVM instructions with a consistent and simplified interface.
 
12
//
 
13
//===----------------------------------------------------------------------===//
 
14
 
 
15
#ifndef LLVM_SUPPORT_IRBUILDER_H
 
16
#define LLVM_SUPPORT_IRBUILDER_H
 
17
 
 
18
#include "llvm/Instructions.h"
 
19
#include "llvm/BasicBlock.h"
 
20
#include "llvm/ADT/Twine.h"
 
21
#include "llvm/Support/ConstantFolder.h"
 
22
 
 
23
namespace llvm {
 
24
  class MDNode;
 
25
 
 
26
/// IRBuilderDefaultInserter - This provides the default implementation of the
 
27
/// IRBuilder 'InsertHelper' method that is called whenever an instruction is
 
28
/// created by IRBuilder and needs to be inserted.  By default, this inserts the
 
29
/// instruction at the insertion point.
 
30
template <bool preserveNames = true>
 
31
class IRBuilderDefaultInserter {
 
32
protected:
 
33
  void InsertHelper(Instruction *I, const Twine &Name,
 
34
                    BasicBlock *BB, BasicBlock::iterator InsertPt) const {
 
35
    if (BB) BB->getInstList().insert(InsertPt, I);
 
36
    if (preserveNames)
 
37
      I->setName(Name);
 
38
  }
 
39
};
 
40
 
 
41
/// IRBuilderBase - Common base class shared among various IRBuilders.
 
42
class IRBuilderBase {
 
43
  unsigned DbgMDKind;
 
44
  MDNode *CurDbgLocation;
 
45
protected:
 
46
  BasicBlock *BB;
 
47
  BasicBlock::iterator InsertPt;
 
48
  LLVMContext &Context;
 
49
public:
 
50
  
 
51
  IRBuilderBase(LLVMContext &context)
 
52
    : DbgMDKind(0), CurDbgLocation(0), Context(context) {
 
53
    ClearInsertionPoint();
 
54
  }
 
55
  
 
56
  //===--------------------------------------------------------------------===//
 
57
  // Builder configuration methods
 
58
  //===--------------------------------------------------------------------===//
 
59
  
 
60
  /// ClearInsertionPoint - Clear the insertion point: created instructions will
 
61
  /// not be inserted into a block.
 
62
  void ClearInsertionPoint() {
 
63
    BB = 0;
 
64
  }
 
65
  
 
66
  BasicBlock *GetInsertBlock() const { return BB; }
 
67
  BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
 
68
  
 
69
  /// SetInsertPoint - This specifies that created instructions should be
 
70
  /// appended to the end of the specified block.
 
71
  void SetInsertPoint(BasicBlock *TheBB) {
 
72
    BB = TheBB;
 
73
    InsertPt = BB->end();
 
74
  }
 
75
  
 
76
  /// SetInsertPoint - This specifies that created instructions should be
 
77
  /// inserted at the specified point.
 
78
  void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
 
79
    BB = TheBB;
 
80
    InsertPt = IP;
 
81
  }
 
82
  
 
83
  /// SetCurrentDebugLocation - Set location information used by debugging
 
84
  /// information.
 
85
  void SetCurrentDebugLocation(MDNode *L);
 
86
  MDNode *getCurrentDebugLocation() const { return CurDbgLocation; }
 
87
  
 
88
  /// SetInstDebugLocation - If this builder has a current debug location, set
 
89
  /// it on the specified instruction.
 
90
  void SetInstDebugLocation(Instruction *I) const;
 
91
 
 
92
  //===--------------------------------------------------------------------===//
 
93
  // Miscellaneous creation methods.
 
94
  //===--------------------------------------------------------------------===//
 
95
  
 
96
  /// CreateGlobalString - Make a new global variable with an initializer that
 
97
  /// has array of i8 type filled in with the nul terminated string value
 
98
  /// specified.  If Name is specified, it is the name of the global variable
 
99
  /// created.
 
100
  Value *CreateGlobalString(const char *Str = "", const Twine &Name = "");
 
101
  
 
102
  //===--------------------------------------------------------------------===//
 
103
  // Type creation methods
 
104
  //===--------------------------------------------------------------------===//
 
105
  
 
106
  /// getInt1Ty - Fetch the type representing a single bit
 
107
  const Type *getInt1Ty() {
 
108
    return Type::getInt1Ty(Context);
 
109
  }
 
110
  
 
111
  /// getInt8Ty - Fetch the type representing an 8-bit integer.
 
112
  const Type *getInt8Ty() {
 
113
    return Type::getInt8Ty(Context);
 
114
  }
 
115
  
 
116
  /// getInt16Ty - Fetch the type representing a 16-bit integer.
 
117
  const Type *getInt16Ty() {
 
118
    return Type::getInt16Ty(Context);
 
119
  }
 
120
  
 
121
  /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
 
122
  const Type *getInt32Ty() {
 
123
    return Type::getInt32Ty(Context);
 
124
  }
 
125
  
 
126
  /// getInt64Ty - Fetch the type representing a 64-bit integer.
 
127
  const Type *getInt64Ty() {
 
128
    return Type::getInt64Ty(Context);
 
129
  }
 
130
  
 
131
  /// getFloatTy - Fetch the type representing a 32-bit floating point value.
 
132
  const Type *getFloatTy() {
 
133
    return Type::getFloatTy(Context);
 
134
  }
 
135
  
 
136
  /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
 
137
  const Type *getDoubleTy() {
 
138
    return Type::getDoubleTy(Context);
 
139
  }
 
140
  
 
141
  /// getVoidTy - Fetch the type representing void.
 
142
  const Type *getVoidTy() {
 
143
    return Type::getVoidTy(Context);
 
144
  }
 
145
  
 
146
  const Type *getInt8PtrTy() {
 
147
    return Type::getInt8PtrTy(Context);
 
148
  }
 
149
  
 
150
  /// getCurrentFunctionReturnType - Get the return type of the current function
 
151
  /// that we're emitting into.
 
152
  const Type *getCurrentFunctionReturnType() const;
 
153
};
 
154
  
 
155
/// IRBuilder - This provides a uniform API for creating instructions and
 
156
/// inserting them into a basic block: either at the end of a BasicBlock, or
 
157
/// at a specific iterator location in a block.
 
158
///
 
159
/// Note that the builder does not expose the full generality of LLVM
 
160
/// instructions.  For access to extra instruction properties, use the mutators
 
161
/// (e.g. setVolatile) on the instructions after they have been created.
 
162
/// The first template argument handles whether or not to preserve names in the
 
163
/// final instruction output. This defaults to on.  The second template argument
 
164
/// specifies a class to use for creating constants.  This defaults to creating
 
165
/// minimally folded constants.  The fourth template argument allows clients to
 
166
/// specify custom insertion hooks that are called on every newly created
 
167
/// insertion.
 
168
template<bool preserveNames = true, typename T = ConstantFolder,
 
169
         typename Inserter = IRBuilderDefaultInserter<preserveNames> >
 
170
class IRBuilder : public IRBuilderBase, public Inserter {
 
171
  T Folder;
 
172
public:
 
173
  IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
 
174
    : IRBuilderBase(C), Inserter(I), Folder(F) {
 
175
  }
 
176
  
 
177
  explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder(C) {
 
178
  }
 
179
  
 
180
  explicit IRBuilder(BasicBlock *TheBB, const T &F)
 
181
    : IRBuilderBase(TheBB->getContext()), Folder(F) {
 
182
    SetInsertPoint(TheBB);
 
183
  }
 
184
  
 
185
  explicit IRBuilder(BasicBlock *TheBB)
 
186
    : IRBuilderBase(TheBB->getContext()), Folder(Context) {
 
187
    SetInsertPoint(TheBB);
 
188
  }
 
189
  
 
190
  IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
 
191
    : IRBuilderBase(TheBB->getContext()), Folder(F) {
 
192
    SetInsertPoint(TheBB, IP);
 
193
  }
 
194
  
 
195
  IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
 
196
    : IRBuilderBase(TheBB->getContext()), Folder(Context) {
 
197
    SetInsertPoint(TheBB, IP);
 
198
  }
 
199
 
 
200
  /// getFolder - Get the constant folder being used.
 
201
  const T &getFolder() { return Folder; }
 
202
 
 
203
  /// isNamePreserving - Return true if this builder is configured to actually
 
204
  /// add the requested names to IR created through it.
 
205
  bool isNamePreserving() const { return preserveNames; }
 
206
  
 
207
  /// Insert - Insert and return the specified instruction.
 
208
  template<typename InstTy>
 
209
  InstTy *Insert(InstTy *I, const Twine &Name = "") const {
 
210
    this->InsertHelper(I, Name, BB, InsertPt);
 
211
    if (getCurrentDebugLocation() != 0)
 
212
      this->SetInstDebugLocation(I);
 
213
    return I;
 
214
  }
 
215
 
 
216
  //===--------------------------------------------------------------------===//
 
217
  // Instruction creation methods: Terminators
 
218
  //===--------------------------------------------------------------------===//
 
219
 
 
220
  /// CreateRetVoid - Create a 'ret void' instruction.
 
221
  ReturnInst *CreateRetVoid() {
 
222
    return Insert(ReturnInst::Create(Context));
 
223
  }
 
224
 
 
225
  /// @verbatim
 
226
  /// CreateRet - Create a 'ret <val>' instruction.
 
227
  /// @endverbatim
 
228
  ReturnInst *CreateRet(Value *V) {
 
229
    return Insert(ReturnInst::Create(Context, V));
 
230
  }
 
231
  
 
232
  /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
 
233
  /// with one Value from the retVals array each, that build a aggregate
 
234
  /// return value one value at a time, and a ret instruction to return
 
235
  /// the resulting aggregate value. This is a convenience function for
 
236
  /// code that uses aggregate return values as a vehicle for having
 
237
  /// multiple return values.
 
238
  ///
 
239
  ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
 
240
    Value *V = UndefValue::get(getCurrentFunctionReturnType());
 
241
    for (unsigned i = 0; i != N; ++i)
 
242
      V = CreateInsertValue(V, retVals[i], i, "mrv");
 
243
    return Insert(ReturnInst::Create(Context, V));
 
244
  }
 
245
 
 
246
  /// CreateBr - Create an unconditional 'br label X' instruction.
 
247
  BranchInst *CreateBr(BasicBlock *Dest) {
 
248
    return Insert(BranchInst::Create(Dest));
 
249
  }
 
250
 
 
251
  /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
 
252
  /// instruction.
 
253
  BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
 
254
    return Insert(BranchInst::Create(True, False, Cond));
 
255
  }
 
256
 
 
257
  /// CreateSwitch - Create a switch instruction with the specified value,
 
258
  /// default dest, and with a hint for the number of cases that will be added
 
259
  /// (for efficient allocation).
 
260
  SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
 
261
    return Insert(SwitchInst::Create(V, Dest, NumCases));
 
262
  }
 
263
 
 
264
  /// CreateIndirectBr - Create an indirect branch instruction with the
 
265
  /// specified address operand, with an optional hint for the number of
 
266
  /// destinations that will be added (for efficient allocation).
 
267
  IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
 
268
    return Insert(IndirectBrInst::Create(Addr, NumDests));
 
269
  }
 
270
 
 
271
  InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
 
272
                           BasicBlock *UnwindDest, const Twine &Name = "") {
 
273
    Value *Args[] = { 0 };
 
274
    return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
 
275
                                     Args), Name);
 
276
  }
 
277
  InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
 
278
                           BasicBlock *UnwindDest, Value *Arg1,
 
279
                           const Twine &Name = "") {
 
280
    Value *Args[] = { Arg1 };
 
281
    return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
 
282
                                     Args+1), Name);
 
283
  }
 
284
  InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
 
285
                            BasicBlock *UnwindDest, Value *Arg1,
 
286
                            Value *Arg2, Value *Arg3,
 
287
                            const Twine &Name = "") {
 
288
    Value *Args[] = { Arg1, Arg2, Arg3 };
 
289
    return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
 
290
                                     Args+3), Name);
 
291
  }
 
292
  /// CreateInvoke - Create an invoke instruction.
 
293
  template<typename InputIterator>
 
294
  InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
 
295
                           BasicBlock *UnwindDest, InputIterator ArgBegin,
 
296
                           InputIterator ArgEnd, const Twine &Name = "") {
 
297
    return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
 
298
                                     ArgBegin, ArgEnd), Name);
 
299
  }
 
300
 
 
301
  UnwindInst *CreateUnwind() {
 
302
    return Insert(new UnwindInst(Context));
 
303
  }
 
304
 
 
305
  UnreachableInst *CreateUnreachable() {
 
306
    return Insert(new UnreachableInst(Context));
 
307
  }
 
308
 
 
309
  //===--------------------------------------------------------------------===//
 
310
  // Instruction creation methods: Binary Operators
 
311
  //===--------------------------------------------------------------------===//
 
312
 
 
313
  Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
 
314
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
315
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
316
        return Folder.CreateAdd(LC, RC);
 
317
    return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
 
318
  }
 
319
  Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
 
320
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
321
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
322
        return Folder.CreateNSWAdd(LC, RC);
 
323
    return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
 
324
  }
 
325
  Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
 
326
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
327
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
328
        return Folder.CreateNUWAdd(LC, RC);
 
329
    return Insert(BinaryOperator::CreateNUWAdd(LHS, RHS), Name);
 
330
  }
 
331
  Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
 
332
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
333
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
334
        return Folder.CreateFAdd(LC, RC);
 
335
    return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
 
336
  }
 
337
  Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "") {
 
338
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
339
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
340
        return Folder.CreateSub(LC, RC);
 
341
    return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
 
342
  }
 
343
  Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
 
344
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
345
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
346
        return Folder.CreateNSWSub(LC, RC);
 
347
    return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
 
348
  }
 
349
  Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
 
350
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
351
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
352
        return Folder.CreateNUWSub(LC, RC);
 
353
    return Insert(BinaryOperator::CreateNUWSub(LHS, RHS), Name);
 
354
  }
 
355
  Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
 
356
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
357
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
358
        return Folder.CreateFSub(LC, RC);
 
359
    return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
 
360
  }
 
361
  Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "") {
 
362
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
363
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
364
        return Folder.CreateMul(LC, RC);
 
365
    return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
 
366
  }
 
367
  Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
 
368
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
369
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
370
        return Folder.CreateNSWMul(LC, RC);
 
371
    return Insert(BinaryOperator::CreateNSWMul(LHS, RHS), Name);
 
372
  }
 
373
  Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
 
374
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
375
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
376
        return Folder.CreateNUWMul(LC, RC);
 
377
    return Insert(BinaryOperator::CreateNUWMul(LHS, RHS), Name);
 
378
  }
 
379
  Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
 
380
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
381
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
382
        return Folder.CreateFMul(LC, RC);
 
383
    return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
 
384
  }
 
385
  Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
 
386
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
387
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
388
        return Folder.CreateUDiv(LC, RC);
 
389
    return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
 
390
  }
 
391
  Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
 
392
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
393
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
394
        return Folder.CreateSDiv(LC, RC);
 
395
    return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
 
396
  }
 
397
  Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
 
398
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
399
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
400
        return Folder.CreateExactSDiv(LC, RC);
 
401
    return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
 
402
  }
 
403
  Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
 
404
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
405
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
406
        return Folder.CreateFDiv(LC, RC);
 
407
    return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
 
408
  }
 
409
  Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
 
410
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
411
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
412
        return Folder.CreateURem(LC, RC);
 
413
    return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
 
414
  }
 
415
  Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
 
416
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
417
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
418
        return Folder.CreateSRem(LC, RC);
 
419
    return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
 
420
  }
 
421
  Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
 
422
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
423
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
424
        return Folder.CreateFRem(LC, RC);
 
425
    return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
 
426
  }
 
427
  Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
 
428
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
429
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
430
        return Folder.CreateShl(LC, RC);
 
431
    return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
 
432
  }
 
433
  Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
 
434
    Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
 
435
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
436
      return Folder.CreateShl(LC, RHSC);
 
437
    return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
 
438
  }
 
439
 
 
440
  Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
 
441
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
442
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
443
        return Folder.CreateLShr(LC, RC);
 
444
    return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
 
445
  }
 
446
  Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
 
447
    Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
 
448
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
449
      return Folder.CreateLShr(LC, RHSC);
 
450
    return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
 
451
  }
 
452
  
 
453
  Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
 
454
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
455
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
456
        return Folder.CreateAShr(LC, RC);
 
457
    return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
 
458
  }
 
459
  Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
 
460
    Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
 
461
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
462
      return Folder.CreateSShr(LC, RHSC);
 
463
    return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
 
464
  }
 
465
 
 
466
  Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
 
467
    if (Constant *RC = dyn_cast<Constant>(RHS)) {
 
468
      if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
 
469
        return LHS;  // LHS & -1 -> LHS
 
470
      if (Constant *LC = dyn_cast<Constant>(LHS))
 
471
        return Folder.CreateAnd(LC, RC);
 
472
    }
 
473
    return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
 
474
  }
 
475
  Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
 
476
    if (Constant *RC = dyn_cast<Constant>(RHS)) {
 
477
      if (RC->isNullValue())
 
478
        return LHS;  // LHS | 0 -> LHS
 
479
      if (Constant *LC = dyn_cast<Constant>(LHS))
 
480
        return Folder.CreateOr(LC, RC);
 
481
    }
 
482
    return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
 
483
  }
 
484
  Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
 
485
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
486
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
487
        return Folder.CreateXor(LC, RC);
 
488
    return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
 
489
  }
 
490
 
 
491
  Value *CreateBinOp(Instruction::BinaryOps Opc,
 
492
                     Value *LHS, Value *RHS, const Twine &Name = "") {
 
493
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
494
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
495
        return Folder.CreateBinOp(Opc, LC, RC);
 
496
    return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
 
497
  }
 
498
 
 
499
  Value *CreateNeg(Value *V, const Twine &Name = "") {
 
500
    if (Constant *VC = dyn_cast<Constant>(V))
 
501
      return Folder.CreateNeg(VC);
 
502
    return Insert(BinaryOperator::CreateNeg(V), Name);
 
503
  }
 
504
  Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
 
505
    if (Constant *VC = dyn_cast<Constant>(V))
 
506
      return Folder.CreateNSWNeg(VC);
 
507
    return Insert(BinaryOperator::CreateNSWNeg(V), Name);
 
508
  }
 
509
  Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
 
510
    if (Constant *VC = dyn_cast<Constant>(V))
 
511
      return Folder.CreateNUWNeg(VC);
 
512
    return Insert(BinaryOperator::CreateNUWNeg(V), Name);
 
513
  }
 
514
  Value *CreateFNeg(Value *V, const Twine &Name = "") {
 
515
    if (Constant *VC = dyn_cast<Constant>(V))
 
516
      return Folder.CreateFNeg(VC);
 
517
    return Insert(BinaryOperator::CreateFNeg(V), Name);
 
518
  }
 
519
  Value *CreateNot(Value *V, const Twine &Name = "") {
 
520
    if (Constant *VC = dyn_cast<Constant>(V))
 
521
      return Folder.CreateNot(VC);
 
522
    return Insert(BinaryOperator::CreateNot(V), Name);
 
523
  }
 
524
 
 
525
  //===--------------------------------------------------------------------===//
 
526
  // Instruction creation methods: Memory Instructions
 
527
  //===--------------------------------------------------------------------===//
 
528
 
 
529
  AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
 
530
                           const Twine &Name = "") {
 
531
    return Insert(new AllocaInst(Ty, ArraySize), Name);
 
532
  }
 
533
  // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
 
534
  // converting the string to 'bool' for the isVolatile parameter.
 
535
  LoadInst *CreateLoad(Value *Ptr, const char *Name) {
 
536
    return Insert(new LoadInst(Ptr), Name);
 
537
  }
 
538
  LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
 
539
    return Insert(new LoadInst(Ptr), Name);
 
540
  }
 
541
  LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
 
542
    return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
 
543
  }
 
544
  StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
 
545
    return Insert(new StoreInst(Val, Ptr, isVolatile));
 
546
  }
 
547
  template<typename InputIterator>
 
548
  Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
 
549
                   const Twine &Name = "") {
 
550
    if (Constant *PC = dyn_cast<Constant>(Ptr)) {
 
551
      // Every index must be constant.
 
552
      InputIterator i;
 
553
      for (i = IdxBegin; i < IdxEnd; ++i)
 
554
        if (!isa<Constant>(*i))
 
555
          break;
 
556
      if (i == IdxEnd)
 
557
        return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
 
558
    }
 
559
    return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
 
560
  }
 
561
  template<typename InputIterator>
 
562
  Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
 
563
                           const Twine &Name = "") {
 
564
    if (Constant *PC = dyn_cast<Constant>(Ptr)) {
 
565
      // Every index must be constant.
 
566
      InputIterator i;
 
567
      for (i = IdxBegin; i < IdxEnd; ++i)
 
568
        if (!isa<Constant>(*i))
 
569
          break;
 
570
      if (i == IdxEnd)
 
571
        return Folder.CreateInBoundsGetElementPtr(PC,
 
572
                                                  &IdxBegin[0],
 
573
                                                  IdxEnd - IdxBegin);
 
574
    }
 
575
    return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
 
576
                  Name);
 
577
  }
 
578
  Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
 
579
    if (Constant *PC = dyn_cast<Constant>(Ptr))
 
580
      if (Constant *IC = dyn_cast<Constant>(Idx))
 
581
        return Folder.CreateGetElementPtr(PC, &IC, 1);
 
582
    return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
 
583
  }
 
584
  Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
 
585
    if (Constant *PC = dyn_cast<Constant>(Ptr))
 
586
      if (Constant *IC = dyn_cast<Constant>(Idx))
 
587
        return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
 
588
    return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
 
589
  }
 
590
  Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
 
591
    Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
 
592
 
 
593
    if (Constant *PC = dyn_cast<Constant>(Ptr))
 
594
      return Folder.CreateGetElementPtr(PC, &Idx, 1);
 
595
 
 
596
    return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);    
 
597
  }
 
598
  Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
 
599
                                    const Twine &Name = "") {
 
600
    Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
 
601
 
 
602
    if (Constant *PC = dyn_cast<Constant>(Ptr))
 
603
      return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
 
604
 
 
605
    return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
 
606
  }
 
607
  Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1, 
 
608
                    const Twine &Name = "") {
 
609
    Value *Idxs[] = {
 
610
      ConstantInt::get(Type::getInt32Ty(Context), Idx0),
 
611
      ConstantInt::get(Type::getInt32Ty(Context), Idx1)
 
612
    };
 
613
 
 
614
    if (Constant *PC = dyn_cast<Constant>(Ptr))
 
615
      return Folder.CreateGetElementPtr(PC, Idxs, 2);
 
616
 
 
617
    return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);    
 
618
  }
 
619
  Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
 
620
                                    const Twine &Name = "") {
 
621
    Value *Idxs[] = {
 
622
      ConstantInt::get(Type::getInt32Ty(Context), Idx0),
 
623
      ConstantInt::get(Type::getInt32Ty(Context), Idx1)
 
624
    };
 
625
 
 
626
    if (Constant *PC = dyn_cast<Constant>(Ptr))
 
627
      return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
 
628
 
 
629
    return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
 
630
  }
 
631
  Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
 
632
    Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
 
633
 
 
634
    if (Constant *PC = dyn_cast<Constant>(Ptr))
 
635
      return Folder.CreateGetElementPtr(PC, &Idx, 1);
 
636
 
 
637
    return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);    
 
638
  }
 
639
  Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
 
640
                                    const Twine &Name = "") {
 
641
    Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
 
642
 
 
643
    if (Constant *PC = dyn_cast<Constant>(Ptr))
 
644
      return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
 
645
 
 
646
    return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
 
647
  }
 
648
  Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
 
649
                    const Twine &Name = "") {
 
650
    Value *Idxs[] = {
 
651
      ConstantInt::get(Type::getInt64Ty(Context), Idx0),
 
652
      ConstantInt::get(Type::getInt64Ty(Context), Idx1)
 
653
    };
 
654
 
 
655
    if (Constant *PC = dyn_cast<Constant>(Ptr))
 
656
      return Folder.CreateGetElementPtr(PC, Idxs, 2);
 
657
 
 
658
    return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);    
 
659
  }
 
660
  Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
 
661
                                    const Twine &Name = "") {
 
662
    Value *Idxs[] = {
 
663
      ConstantInt::get(Type::getInt64Ty(Context), Idx0),
 
664
      ConstantInt::get(Type::getInt64Ty(Context), Idx1)
 
665
    };
 
666
 
 
667
    if (Constant *PC = dyn_cast<Constant>(Ptr))
 
668
      return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
 
669
 
 
670
    return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
 
671
  }
 
672
  Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
 
673
    return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
 
674
  }
 
675
  
 
676
  /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
 
677
  /// with "i8*" type instead of a pointer to array of i8.
 
678
  Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
 
679
    Value *gv = CreateGlobalString(Str, Name);
 
680
    Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
 
681
    Value *Args[] = { zero, zero };
 
682
    return CreateInBoundsGEP(gv, Args, Args+2, Name);
 
683
  }
 
684
  
 
685
  //===--------------------------------------------------------------------===//
 
686
  // Instruction creation methods: Cast/Conversion Operators
 
687
  //===--------------------------------------------------------------------===//
 
688
 
 
689
  Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
 
690
    return CreateCast(Instruction::Trunc, V, DestTy, Name);
 
691
  }
 
692
  Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
 
693
    return CreateCast(Instruction::ZExt, V, DestTy, Name);
 
694
  }
 
695
  Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
 
696
    return CreateCast(Instruction::SExt, V, DestTy, Name);
 
697
  }
 
698
  Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
 
699
    return CreateCast(Instruction::FPToUI, V, DestTy, Name);
 
700
  }
 
701
  Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
 
702
    return CreateCast(Instruction::FPToSI, V, DestTy, Name);
 
703
  }
 
704
  Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
 
705
    return CreateCast(Instruction::UIToFP, V, DestTy, Name);
 
706
  }
 
707
  Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
 
708
    return CreateCast(Instruction::SIToFP, V, DestTy, Name);
 
709
  }
 
710
  Value *CreateFPTrunc(Value *V, const Type *DestTy,
 
711
                       const Twine &Name = "") {
 
712
    return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
 
713
  }
 
714
  Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
 
715
    return CreateCast(Instruction::FPExt, V, DestTy, Name);
 
716
  }
 
717
  Value *CreatePtrToInt(Value *V, const Type *DestTy,
 
718
                        const Twine &Name = "") {
 
719
    return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
 
720
  }
 
721
  Value *CreateIntToPtr(Value *V, const Type *DestTy,
 
722
                        const Twine &Name = "") {
 
723
    return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
 
724
  }
 
725
  Value *CreateBitCast(Value *V, const Type *DestTy,
 
726
                       const Twine &Name = "") {
 
727
    return CreateCast(Instruction::BitCast, V, DestTy, Name);
 
728
  }
 
729
  Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
 
730
                             const Twine &Name = "") {
 
731
    if (V->getType() == DestTy)
 
732
      return V;
 
733
    if (Constant *VC = dyn_cast<Constant>(V))
 
734
      return Folder.CreateZExtOrBitCast(VC, DestTy);
 
735
    return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
 
736
  }
 
737
  Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
 
738
                             const Twine &Name = "") {
 
739
    if (V->getType() == DestTy)
 
740
      return V;
 
741
    if (Constant *VC = dyn_cast<Constant>(V))
 
742
      return Folder.CreateSExtOrBitCast(VC, DestTy);
 
743
    return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
 
744
  }
 
745
  Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
 
746
                              const Twine &Name = "") {
 
747
    if (V->getType() == DestTy)
 
748
      return V;
 
749
    if (Constant *VC = dyn_cast<Constant>(V))
 
750
      return Folder.CreateTruncOrBitCast(VC, DestTy);
 
751
    return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
 
752
  }
 
753
  Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
 
754
                    const Twine &Name = "") {
 
755
    if (V->getType() == DestTy)
 
756
      return V;
 
757
    if (Constant *VC = dyn_cast<Constant>(V))
 
758
      return Folder.CreateCast(Op, VC, DestTy);
 
759
    return Insert(CastInst::Create(Op, V, DestTy), Name);
 
760
  }
 
761
  Value *CreatePointerCast(Value *V, const Type *DestTy,
 
762
                           const Twine &Name = "") {
 
763
    if (V->getType() == DestTy)
 
764
      return V;
 
765
    if (Constant *VC = dyn_cast<Constant>(V))
 
766
      return Folder.CreatePointerCast(VC, DestTy);
 
767
    return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
 
768
  }
 
769
  Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
 
770
                       const Twine &Name = "") {
 
771
    if (V->getType() == DestTy)
 
772
      return V;
 
773
    if (Constant *VC = dyn_cast<Constant>(V))
 
774
      return Folder.CreateIntCast(VC, DestTy, isSigned);
 
775
    return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
 
776
  }
 
777
private:
 
778
  // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
 
779
  // error, instead of converting the string to bool for the isSigned parameter.
 
780
  Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
 
781
public:
 
782
  Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
 
783
    if (V->getType() == DestTy)
 
784
      return V;
 
785
    if (Constant *VC = dyn_cast<Constant>(V))
 
786
      return Folder.CreateFPCast(VC, DestTy);
 
787
    return Insert(CastInst::CreateFPCast(V, DestTy), Name);
 
788
  }
 
789
 
 
790
  //===--------------------------------------------------------------------===//
 
791
  // Instruction creation methods: Compare Instructions
 
792
  //===--------------------------------------------------------------------===//
 
793
 
 
794
  Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
 
795
    return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
 
796
  }
 
797
  Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
 
798
    return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
 
799
  }
 
800
  Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
 
801
    return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
 
802
  }
 
803
  Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
 
804
    return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
 
805
  }
 
806
  Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
 
807
    return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
 
808
  }
 
809
  Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
 
810
    return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
 
811
  }
 
812
  Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
 
813
    return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
 
814
  }
 
815
  Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
 
816
    return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
 
817
  }
 
818
  Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
 
819
    return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
 
820
  }
 
821
  Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
 
822
    return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
 
823
  }
 
824
 
 
825
  Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
 
826
    return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
 
827
  }
 
828
  Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
 
829
    return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
 
830
  }
 
831
  Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
 
832
    return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
 
833
  }
 
834
  Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
 
835
    return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
 
836
  }
 
837
  Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
 
838
    return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
 
839
  }
 
840
  Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
 
841
    return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
 
842
  }
 
843
  Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
 
844
    return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
 
845
  }
 
846
  Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
 
847
    return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
 
848
  }
 
849
  Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
 
850
    return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
 
851
  }
 
852
  Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
 
853
    return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
 
854
  }
 
855
  Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
 
856
    return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
 
857
  }
 
858
  Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
 
859
    return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
 
860
  }
 
861
  Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
 
862
    return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
 
863
  }
 
864
  Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
 
865
    return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
 
866
  }
 
867
 
 
868
  Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
 
869
                    const Twine &Name = "") {
 
870
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
871
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
872
        return Folder.CreateICmp(P, LC, RC);
 
873
    return Insert(new ICmpInst(P, LHS, RHS), Name);
 
874
  }
 
875
  Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
 
876
                    const Twine &Name = "") {
 
877
    if (Constant *LC = dyn_cast<Constant>(LHS))
 
878
      if (Constant *RC = dyn_cast<Constant>(RHS))
 
879
        return Folder.CreateFCmp(P, LC, RC);
 
880
    return Insert(new FCmpInst(P, LHS, RHS), Name);
 
881
  }
 
882
 
 
883
  //===--------------------------------------------------------------------===//
 
884
  // Instruction creation methods: Other Instructions
 
885
  //===--------------------------------------------------------------------===//
 
886
 
 
887
  PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
 
888
    return Insert(PHINode::Create(Ty), Name);
 
889
  }
 
890
 
 
891
  CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
 
892
    return Insert(CallInst::Create(Callee), Name);
 
893
  }
 
894
  CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
 
895
    return Insert(CallInst::Create(Callee, Arg), Name);
 
896
  }
 
897
  CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
 
898
                        const Twine &Name = "") {
 
899
    Value *Args[] = { Arg1, Arg2 };
 
900
    return Insert(CallInst::Create(Callee, Args, Args+2), Name);
 
901
  }
 
902
  CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
 
903
                        const Twine &Name = "") {
 
904
    Value *Args[] = { Arg1, Arg2, Arg3 };
 
905
    return Insert(CallInst::Create(Callee, Args, Args+3), Name);
 
906
  }
 
907
  CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
 
908
                        Value *Arg4, const Twine &Name = "") {
 
909
    Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
 
910
    return Insert(CallInst::Create(Callee, Args, Args+4), Name);
 
911
  }
 
912
 
 
913
  template<typename InputIterator>
 
914
  CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
 
915
                       InputIterator ArgEnd, const Twine &Name = "") {
 
916
    return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
 
917
  }
 
918
 
 
919
  Value *CreateSelect(Value *C, Value *True, Value *False,
 
920
                      const Twine &Name = "") {
 
921
    if (Constant *CC = dyn_cast<Constant>(C))
 
922
      if (Constant *TC = dyn_cast<Constant>(True))
 
923
        if (Constant *FC = dyn_cast<Constant>(False))
 
924
          return Folder.CreateSelect(CC, TC, FC);
 
925
    return Insert(SelectInst::Create(C, True, False), Name);
 
926
  }
 
927
 
 
928
  VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
 
929
    return Insert(new VAArgInst(List, Ty), Name);
 
930
  }
 
931
 
 
932
  Value *CreateExtractElement(Value *Vec, Value *Idx,
 
933
                              const Twine &Name = "") {
 
934
    if (Constant *VC = dyn_cast<Constant>(Vec))
 
935
      if (Constant *IC = dyn_cast<Constant>(Idx))
 
936
        return Folder.CreateExtractElement(VC, IC);
 
937
    return Insert(ExtractElementInst::Create(Vec, Idx), Name);
 
938
  }
 
939
 
 
940
  Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
 
941
                             const Twine &Name = "") {
 
942
    if (Constant *VC = dyn_cast<Constant>(Vec))
 
943
      if (Constant *NC = dyn_cast<Constant>(NewElt))
 
944
        if (Constant *IC = dyn_cast<Constant>(Idx))
 
945
          return Folder.CreateInsertElement(VC, NC, IC);
 
946
    return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
 
947
  }
 
948
 
 
949
  Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
 
950
                             const Twine &Name = "") {
 
951
    if (Constant *V1C = dyn_cast<Constant>(V1))
 
952
      if (Constant *V2C = dyn_cast<Constant>(V2))
 
953
        if (Constant *MC = dyn_cast<Constant>(Mask))
 
954
          return Folder.CreateShuffleVector(V1C, V2C, MC);
 
955
    return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
 
956
  }
 
957
 
 
958
  Value *CreateExtractValue(Value *Agg, unsigned Idx,
 
959
                            const Twine &Name = "") {
 
960
    if (Constant *AggC = dyn_cast<Constant>(Agg))
 
961
      return Folder.CreateExtractValue(AggC, &Idx, 1);
 
962
    return Insert(ExtractValueInst::Create(Agg, Idx), Name);
 
963
  }
 
964
 
 
965
  template<typename InputIterator>
 
966
  Value *CreateExtractValue(Value *Agg,
 
967
                            InputIterator IdxBegin,
 
968
                            InputIterator IdxEnd,
 
969
                            const Twine &Name = "") {
 
970
    if (Constant *AggC = dyn_cast<Constant>(Agg))
 
971
      return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
 
972
    return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
 
973
  }
 
974
 
 
975
  Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
 
976
                           const Twine &Name = "") {
 
977
    if (Constant *AggC = dyn_cast<Constant>(Agg))
 
978
      if (Constant *ValC = dyn_cast<Constant>(Val))
 
979
        return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
 
980
    return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
 
981
  }
 
982
 
 
983
  template<typename InputIterator>
 
984
  Value *CreateInsertValue(Value *Agg, Value *Val,
 
985
                           InputIterator IdxBegin,
 
986
                           InputIterator IdxEnd,
 
987
                           const Twine &Name = "") {
 
988
    if (Constant *AggC = dyn_cast<Constant>(Agg))
 
989
      if (Constant *ValC = dyn_cast<Constant>(Val))
 
990
        return Folder.CreateInsertValue(AggC, ValC, IdxBegin, IdxEnd-IdxBegin);
 
991
    return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
 
992
  }
 
993
 
 
994
  //===--------------------------------------------------------------------===//
 
995
  // Utility creation methods
 
996
  //===--------------------------------------------------------------------===//
 
997
 
 
998
  /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
 
999
  Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
 
1000
    return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
 
1001
                        Name);
 
1002
  }
 
1003
 
 
1004
  /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
 
1005
  Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
 
1006
    return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
 
1007
                        Name);
 
1008
  }
 
1009
 
 
1010
  /// CreatePtrDiff - Return the i64 difference between two pointer values,
 
1011
  /// dividing out the size of the pointed-to objects.  This is intended to
 
1012
  /// implement C-style pointer subtraction. As such, the pointers must be
 
1013
  /// appropriately aligned for their element types and pointing into the
 
1014
  /// same object.
 
1015
  Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
 
1016
    assert(LHS->getType() == RHS->getType() &&
 
1017
           "Pointer subtraction operand types must match!");
 
1018
    const PointerType *ArgType = cast<PointerType>(LHS->getType());
 
1019
    Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
 
1020
    Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
 
1021
    Value *Difference = CreateSub(LHS_int, RHS_int);
 
1022
    return CreateExactSDiv(Difference,
 
1023
                           ConstantExpr::getSizeOf(ArgType->getElementType()),
 
1024
                           Name);
 
1025
  }
 
1026
};
 
1027
 
 
1028
}
 
1029
 
 
1030
#endif