1 //===-- llvm/Support/LLVMBuilder.h - Builder for LLVM Instrs ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the LLVMBuilder class, which is used as a convenient way
11 // to create LLVM instructions with a consistent and simplified interface.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_LLVMBUILDER_H
16 #define LLVM_SUPPORT_LLVMBUILDER_H
18 #include "llvm/BasicBlock.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Constants.h"
24 /// LLVMBuilder - This provides a uniform API for creating instructions and
25 /// inserting them into a basic block: either at the end of a BasicBlock, or
26 /// at a specific iterator location in a block.
28 /// Note that the builder does not expose the full generality of LLVM
29 /// instructions. For example, it cannot be used to create instructions with
30 /// arbitrary names (specifically, names with nul characters in them) - It only
31 /// supports nul-terminated C strings. For fully generic names, use
32 /// I->setName(). For access to extra instruction properties, use the mutators
33 /// (e.g. setVolatile) on the instructions after they have been created.
36 BasicBlock::iterator InsertPt;
38 LLVMBuilder() { ClearInsertionPoint(); }
39 explicit LLVMBuilder(BasicBlock *TheBB) { SetInsertPoint(TheBB); }
40 LLVMBuilder(BasicBlock *TheBB, BasicBlock::iterator IP) {
41 SetInsertPoint(TheBB, IP);
44 //===--------------------------------------------------------------------===//
45 // Builder configuration methods
46 //===--------------------------------------------------------------------===//
48 /// ClearInsertionPoint - Clear the insertion point: created instructions will
49 /// not be inserted into a block.
50 void ClearInsertionPoint() {
54 BasicBlock *GetInsertBlock() const { return BB; }
56 /// SetInsertPoint - This specifies that created instructions should be
57 /// appended to the end of the specified block.
58 void SetInsertPoint(BasicBlock *TheBB) {
63 /// SetInsertPoint - This specifies that created instructions should be
64 /// inserted at the specified point.
65 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
70 /// Insert - Insert and return the specified instruction.
71 template<typename InstTy>
72 InstTy *Insert(InstTy *I) const {
77 /// InsertHelper - Insert the specified instruction at the specified insertion
78 /// point. This is split out of Insert so that it isn't duplicated for every
79 /// template instantiation.
80 void InsertHelper(Instruction *I) const {
81 if (BB) BB->getInstList().insert(InsertPt, I);
84 //===--------------------------------------------------------------------===//
85 // Instruction creation methods: Terminators
86 //===--------------------------------------------------------------------===//
88 /// CreateRetVoid - Create a 'ret void' instruction.
89 ReturnInst *CreateRetVoid() {
90 return Insert(new ReturnInst());
94 /// CreateRet - Create a 'ret <val>' instruction.
96 ReturnInst *CreateRet(Value *V) {
97 return Insert(new ReturnInst(V));
100 /// CreateBr - Create an unconditional 'br label X' instruction.
101 BranchInst *CreateBr(BasicBlock *Dest) {
102 return Insert(new BranchInst(Dest));
105 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
107 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
108 return Insert(new BranchInst(True, False, Cond));
111 /// CreateSwitch - Create a switch instruction with the specified value,
112 /// default dest, and with a hint for the number of cases that will be added
113 /// (for efficient allocation).
114 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
115 return Insert(new SwitchInst(V, Dest, NumCases));
118 /// CreateInvoke - Create an invoke instruction.
119 template<typename InputIterator>
120 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
121 BasicBlock *UnwindDest, InputIterator ArgBegin,
122 InputIterator ArgEnd, const char *Name = "") {
123 return(Insert(new InvokeInst(Callee, NormalDest, UnwindDest,
124 ArgBegin, ArgEnd, Name)));
127 UnwindInst *CreateUnwind() {
128 return Insert(new UnwindInst());
131 UnreachableInst *CreateUnreachable() {
132 return Insert(new UnreachableInst());
135 //===--------------------------------------------------------------------===//
136 // Instruction creation methods: Binary Operators
137 //===--------------------------------------------------------------------===//
139 BinaryOperator *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
140 return Insert(BinaryOperator::createAdd(LHS, RHS, Name));
142 BinaryOperator *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
143 return Insert(BinaryOperator::createSub(LHS, RHS, Name));
145 BinaryOperator *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
146 return Insert(BinaryOperator::createMul(LHS, RHS, Name));
148 BinaryOperator *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
149 return Insert(BinaryOperator::createUDiv(LHS, RHS, Name));
151 BinaryOperator *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
152 return Insert(BinaryOperator::createSDiv(LHS, RHS, Name));
154 BinaryOperator *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
155 return Insert(BinaryOperator::createFDiv(LHS, RHS, Name));
157 BinaryOperator *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
158 return Insert(BinaryOperator::createURem(LHS, RHS, Name));
160 BinaryOperator *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
161 return Insert(BinaryOperator::createSRem(LHS, RHS, Name));
163 BinaryOperator *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
164 return Insert(BinaryOperator::createFRem(LHS, RHS, Name));
166 BinaryOperator *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
167 return Insert(BinaryOperator::createShl(LHS, RHS, Name));
169 BinaryOperator *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
170 return Insert(BinaryOperator::createLShr(LHS, RHS, Name));
172 BinaryOperator *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
173 return Insert(BinaryOperator::createAShr(LHS, RHS, Name));
175 BinaryOperator *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
176 return Insert(BinaryOperator::createAnd(LHS, RHS, Name));
178 BinaryOperator *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
179 return Insert(BinaryOperator::createOr(LHS, RHS, Name));
181 BinaryOperator *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
182 return Insert(BinaryOperator::createXor(LHS, RHS, Name));
185 BinaryOperator *CreateBinOp(Instruction::BinaryOps Opc,
186 Value *LHS, Value *RHS, const char *Name = "") {
187 return Insert(BinaryOperator::create(Opc, LHS, RHS, Name));
190 BinaryOperator *CreateNeg(Value *V, const char *Name = "") {
191 return Insert(BinaryOperator::createNeg(V, Name));
193 BinaryOperator *CreateNot(Value *V, const char *Name = "") {
194 return Insert(BinaryOperator::createNot(V, Name));
197 //===--------------------------------------------------------------------===//
198 // Instruction creation methods: Memory Instructions
199 //===--------------------------------------------------------------------===//
201 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
202 const char *Name = "") {
203 return Insert(new MallocInst(Ty, ArraySize, Name));
205 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
206 const char *Name = "") {
207 return Insert(new AllocaInst(Ty, ArraySize, Name));
209 FreeInst *CreateFree(Value *Ptr) {
210 return Insert(new FreeInst(Ptr));
212 LoadInst *CreateLoad(Value *Ptr, const char *Name = 0) {
213 return Insert(new LoadInst(Ptr, Name));
215 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = 0) {
216 return Insert(new LoadInst(Ptr, Name, isVolatile));
218 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
219 return Insert(new StoreInst(Val, Ptr, isVolatile));
221 template<typename InputIterator>
222 GetElementPtrInst *CreateGEP(Value *Ptr, InputIterator IdxBegin,
223 InputIterator IdxEnd, const char *Name = "") {
224 return(Insert(new GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Name)));
226 GetElementPtrInst *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
227 return Insert(new GetElementPtrInst(Ptr, Idx, Name));
229 GetElementPtrInst *CreateStructGEP(Value *Ptr, unsigned Idx,
230 const char *Name = "") {
231 llvm::Value *Idxs[] = {
232 ConstantInt::get(llvm::Type::Int32Ty, 0),
233 ConstantInt::get(llvm::Type::Int32Ty, Idx)
235 return Insert(new GetElementPtrInst(Ptr, Idxs, Idxs+2, Name));
238 //===--------------------------------------------------------------------===//
239 // Instruction creation methods: Cast/Conversion Operators
240 //===--------------------------------------------------------------------===//
242 TruncInst *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
243 return Insert(new TruncInst(V, DestTy, Name));
245 ZExtInst *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
246 return Insert(new ZExtInst(V, DestTy, Name));
248 SExtInst *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
249 return Insert(new SExtInst(V, DestTy, Name));
251 FPToUIInst *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
252 return Insert(new FPToUIInst(V, DestTy, Name));
254 FPToSIInst *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
255 return Insert(new FPToSIInst(V, DestTy, Name));
257 UIToFPInst *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
258 return Insert(new UIToFPInst(V, DestTy, Name));
260 SIToFPInst *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
261 return Insert(new SIToFPInst(V, DestTy, Name));
263 FPTruncInst *CreateFPTrunc(Value *V, const Type *DestTy,
264 const char *Name = "") {
265 return Insert(new FPTruncInst(V, DestTy, Name));
267 FPExtInst *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
268 return Insert(new FPExtInst(V, DestTy, Name));
270 PtrToIntInst *CreatePtrToInt(Value *V, const Type *DestTy,
271 const char *Name = "") {
272 return Insert(new PtrToIntInst(V, DestTy, Name));
274 IntToPtrInst *CreateIntToPtr(Value *V, const Type *DestTy,
275 const char *Name = "") {
276 return Insert(new IntToPtrInst(V, DestTy, Name));
278 BitCastInst *CreateBitCast(Value *V, const Type *DestTy,
279 const char *Name = "") {
280 return Insert(new BitCastInst(V, DestTy, Name));
283 CastInst *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
284 const char *Name = "") {
285 return Insert(CastInst::create(Op, V, DestTy, Name));
287 CastInst *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
288 const char *Name = "") {
289 return Insert(CastInst::createIntegerCast(V, DestTy, isSigned, Name));
294 //===--------------------------------------------------------------------===//
295 // Instruction creation methods: Compare Instructions
296 //===--------------------------------------------------------------------===//
298 ICmpInst *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
299 return Insert(new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS, Name));
301 ICmpInst *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
302 return Insert(new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS, Name));
304 ICmpInst *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
305 return Insert(new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, Name));
307 ICmpInst *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
308 return Insert(new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS, Name));
310 ICmpInst *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
311 return Insert(new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS, Name));
313 ICmpInst *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
314 return Insert(new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS, Name));
316 ICmpInst *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
317 return Insert(new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, Name));
319 ICmpInst *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
320 return Insert(new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS, Name));
322 ICmpInst *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
323 return Insert(new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS, Name));
325 ICmpInst *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
326 return Insert(new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS, Name));
329 FCmpInst *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
330 return Insert(new FCmpInst(FCmpInst::FCMP_OEQ, LHS, RHS, Name));
332 FCmpInst *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
333 return Insert(new FCmpInst(FCmpInst::FCMP_OGT, LHS, RHS, Name));
335 FCmpInst *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
336 return Insert(new FCmpInst(FCmpInst::FCMP_OGE, LHS, RHS, Name));
338 FCmpInst *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
339 return Insert(new FCmpInst(FCmpInst::FCMP_OLT, LHS, RHS, Name));
341 FCmpInst *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
342 return Insert(new FCmpInst(FCmpInst::FCMP_OLE, LHS, RHS, Name));
344 FCmpInst *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
345 return Insert(new FCmpInst(FCmpInst::FCMP_ONE, LHS, RHS, Name));
347 FCmpInst *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
348 return Insert(new FCmpInst(FCmpInst::FCMP_ORD, LHS, RHS, Name));
350 FCmpInst *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
351 return Insert(new FCmpInst(FCmpInst::FCMP_UNO, LHS, RHS, Name));
353 FCmpInst *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
354 return Insert(new FCmpInst(FCmpInst::FCMP_UEQ, LHS, RHS, Name));
356 FCmpInst *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
357 return Insert(new FCmpInst(FCmpInst::FCMP_UGT, LHS, RHS, Name));
359 FCmpInst *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
360 return Insert(new FCmpInst(FCmpInst::FCMP_UGE, LHS, RHS, Name));
362 FCmpInst *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
363 return Insert(new FCmpInst(FCmpInst::FCMP_ULT, LHS, RHS, Name));
365 FCmpInst *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
366 return Insert(new FCmpInst(FCmpInst::FCMP_ULE, LHS, RHS, Name));
368 FCmpInst *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
369 return Insert(new FCmpInst(FCmpInst::FCMP_UNE, LHS, RHS, Name));
373 ICmpInst *CreateICmp(ICmpInst::Predicate P, Value *LHS, Value *RHS,
374 const char *Name = "") {
375 return Insert(new ICmpInst(P, LHS, RHS, Name));
377 FCmpInst *CreateFCmp(FCmpInst::Predicate P, Value *LHS, Value *RHS,
378 const char *Name = "") {
379 return Insert(new FCmpInst(P, LHS, RHS, Name));
382 //===--------------------------------------------------------------------===//
383 // Instruction creation methods: Other Instructions
384 //===--------------------------------------------------------------------===//
386 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
387 return Insert(new PHINode(Ty, Name));
390 CallInst *CreateCall(Value *Callee, const char *Name = "") {
391 return Insert(new CallInst(Callee, Name));
393 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
394 return Insert(new CallInst(Callee, Arg, Name));
397 template<typename InputIterator>
398 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
399 InputIterator ArgEnd, const char *Name = "") {
400 return(Insert(new CallInst(Callee, ArgBegin, ArgEnd, Name)));
403 SelectInst *CreateSelect(Value *C, Value *True, Value *False,
404 const char *Name = "") {
405 return Insert(new SelectInst(C, True, False, Name));
408 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
409 return Insert(new VAArgInst(List, Ty, Name));
412 ExtractElementInst *CreateExtractElement(Value *Vec, Value *Idx,
413 const char *Name = "") {
414 return Insert(new ExtractElementInst(Vec, Idx, Name));
417 InsertElementInst *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
418 const char *Name = "") {
419 return Insert(new InsertElementInst(Vec, NewElt, Idx, Name));
422 ShuffleVectorInst *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
423 const char *Name = "") {
424 return Insert(new ShuffleVectorInst(V1, V2, Mask, Name));
428 /// LLVMFoldingBuilder - A version of LLVMBuilder that constant folds operands
430 class LLVMFoldingBuilder : public LLVMBuilder {
433 LLVMFoldingBuilder() {}
434 explicit LLVMFoldingBuilder(BasicBlock *TheBB)
435 : LLVMBuilder(TheBB) {}
436 LLVMFoldingBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
437 : LLVMBuilder(TheBB, IP) {}
439 //===--------------------------------------------------------------------===//
440 // Instruction creation methods: Binary Operators
441 //===--------------------------------------------------------------------===//
443 Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
444 if (Constant *LC = dyn_cast<Constant>(LHS))
445 if (Constant *RC = dyn_cast<Constant>(RHS))
446 return ConstantExpr::getAdd(LC, RC);
447 return LLVMBuilder::CreateAdd(LHS, RHS, Name);
450 Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
451 if (Constant *LC = dyn_cast<Constant>(LHS))
452 if (Constant *RC = dyn_cast<Constant>(RHS))
453 return ConstantExpr::getSub(LC, RC);
454 return LLVMBuilder::CreateSub(LHS, RHS, Name);
457 Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
458 if (Constant *LC = dyn_cast<Constant>(LHS))
459 if (Constant *RC = dyn_cast<Constant>(RHS))
460 return ConstantExpr::getMul(LC, RC);
461 return LLVMBuilder::CreateMul(LHS, RHS, Name);
464 Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
465 if (Constant *LC = dyn_cast<Constant>(LHS))
466 if (Constant *RC = dyn_cast<Constant>(RHS))
467 return ConstantExpr::getUDiv(LC, RC);
468 return LLVMBuilder::CreateUDiv(LHS, RHS, Name);
471 Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
472 if (Constant *LC = dyn_cast<Constant>(LHS))
473 if (Constant *RC = dyn_cast<Constant>(RHS))
474 return ConstantExpr::getSDiv(LC, RC);
475 return LLVMBuilder::CreateSDiv(LHS, RHS, Name);
478 Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
479 if (Constant *LC = dyn_cast<Constant>(LHS))
480 if (Constant *RC = dyn_cast<Constant>(RHS))
481 return ConstantExpr::getFDiv(LC, RC);
482 return LLVMBuilder::CreateFDiv(LHS, RHS, Name);
485 Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
486 if (Constant *LC = dyn_cast<Constant>(LHS))
487 if (Constant *RC = dyn_cast<Constant>(RHS))
488 return ConstantExpr::getURem(LC, RC);
489 return LLVMBuilder::CreateURem(LHS, RHS, Name);
492 Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
493 if (Constant *LC = dyn_cast<Constant>(LHS))
494 if (Constant *RC = dyn_cast<Constant>(RHS))
495 return ConstantExpr::getSRem(LC, RC);
496 return LLVMBuilder::CreateSRem(LHS, RHS, Name);
499 Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
500 if (Constant *LC = dyn_cast<Constant>(LHS))
501 if (Constant *RC = dyn_cast<Constant>(RHS))
502 return ConstantExpr::getFRem(LC, RC);
503 return LLVMBuilder::CreateFRem(LHS, RHS, Name);
506 Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
507 if (Constant *LC = dyn_cast<Constant>(LHS))
508 if (Constant *RC = dyn_cast<Constant>(RHS))
509 return ConstantExpr::getAnd(LC, RC);
510 return LLVMBuilder::CreateAnd(LHS, RHS, Name);
513 Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
514 if (Constant *LC = dyn_cast<Constant>(LHS))
515 if (Constant *RC = dyn_cast<Constant>(RHS))
516 return ConstantExpr::getOr(LC, RC);
517 return LLVMBuilder::CreateOr(LHS, RHS, Name);
520 Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
521 if (Constant *LC = dyn_cast<Constant>(LHS))
522 if (Constant *RC = dyn_cast<Constant>(RHS))
523 return ConstantExpr::getXor(LC, RC);
524 return LLVMBuilder::CreateXor(LHS, RHS, Name);
527 Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
528 if (Constant *LC = dyn_cast<Constant>(LHS))
529 if (Constant *RC = dyn_cast<Constant>(RHS))
530 return ConstantExpr::getShl(LC, RC);
531 return LLVMBuilder::CreateShl(LHS, RHS, Name);
534 Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
535 if (Constant *LC = dyn_cast<Constant>(LHS))
536 if (Constant *RC = dyn_cast<Constant>(RHS))
537 return ConstantExpr::getLShr(LC, RC);
538 return LLVMBuilder::CreateLShr(LHS, RHS, Name);
541 Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
542 if (Constant *LC = dyn_cast<Constant>(LHS))
543 if (Constant *RC = dyn_cast<Constant>(RHS))
544 return ConstantExpr::getAShr(LC, RC);
545 return LLVMBuilder::CreateAShr(LHS, RHS, Name);
548 //===--------------------------------------------------------------------===//
549 // Instruction creation methods: Memory Instructions
550 //===--------------------------------------------------------------------===//
552 template<typename InputIterator>
553 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin,
554 InputIterator IdxEnd, const char *Name = "") {
556 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
557 // Every index must be constant.
559 for (i = IdxBegin; i < IdxEnd; ++i)
560 if (!dyn_cast<Constant>(*i))
563 return ConstantExpr::getGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
565 return LLVMBuilder::CreateGEP(Ptr, IdxBegin, IdxEnd, Name);
567 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
568 if (Constant *PC = dyn_cast<Constant>(Ptr))
569 if (Constant *IC = dyn_cast<Constant>(Idx))
570 return ConstantExpr::getGetElementPtr(PC, &IC, 1);
571 return LLVMBuilder::CreateGEP(Ptr, Idx, Name);
574 //===--------------------------------------------------------------------===//
575 // Instruction creation methods: Cast/Conversion Operators
576 //===--------------------------------------------------------------------===//
578 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
579 return CreateCast(Instruction::Trunc, V, DestTy, Name);
581 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
582 return CreateCast(Instruction::ZExt, V, DestTy, Name);
584 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
585 return CreateCast(Instruction::SExt, V, DestTy, Name);
587 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
588 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
590 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
591 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
593 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
594 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
596 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
597 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
599 Value *CreateFPTrunc(Value *V, const Type *DestTy,
600 const char *Name = "") {
601 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
603 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
604 return CreateCast(Instruction::FPExt, V, DestTy, Name);
606 Value *CreatePtrToInt(Value *V, const Type *DestTy,
607 const char *Name = "") {
608 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
610 Value *CreateIntToPtr(Value *V, const Type *DestTy,
611 const char *Name = "") {
612 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
614 Value *CreateBitCast(Value *V, const Type *DestTy,
615 const char *Name = "") {
616 return CreateCast(Instruction::BitCast, V, DestTy, Name);
619 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
620 const char *Name = "") {
621 if (Constant *VC = dyn_cast<Constant>(V))
622 return ConstantExpr::getCast(Op, VC, DestTy);
623 return LLVMBuilder::CreateCast(Op, V, DestTy, Name);
625 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
626 const char *Name = "") {
627 if (Constant *VC = dyn_cast<Constant>(V))
628 return ConstantExpr::getIntegerCast(VC, DestTy, isSigned);
629 return LLVMBuilder::CreateIntCast(V, DestTy, isSigned, Name);
632 //===--------------------------------------------------------------------===//
633 // Instruction creation methods: Compare Instructions
634 //===--------------------------------------------------------------------===//
636 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
637 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
639 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
640 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
642 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
643 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
645 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
646 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
648 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
649 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
651 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
652 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
654 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
655 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
657 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
658 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
660 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
661 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
663 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
664 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
667 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
668 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
670 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
671 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
673 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
674 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
676 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
677 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
679 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
680 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
682 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
683 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
685 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
686 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
688 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
689 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
691 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
692 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
694 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
695 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
697 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
698 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
700 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
701 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
703 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
704 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
706 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
707 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
710 Value *CreateICmp(ICmpInst::Predicate P, Value *LHS, Value *RHS,
711 const char *Name = "") {
712 if (Constant *LC = dyn_cast<Constant>(LHS))
713 if (Constant *RC = dyn_cast<Constant>(RHS))
714 return ConstantExpr::getCompare(P, LC, RC);
715 return LLVMBuilder::CreateICmp(P, LHS, RHS, Name);
718 Value *CreateFCmp(FCmpInst::Predicate P, Value *LHS, Value *RHS,
719 const char *Name = "") {
720 if (Constant *LC = dyn_cast<Constant>(LHS))
721 if (Constant *RC = dyn_cast<Constant>(RHS))
722 return ConstantExpr::getCompare(P, LC, RC);
723 return LLVMBuilder::CreateFCmp(P, LHS, RHS, Name);
726 //===--------------------------------------------------------------------===//
727 // Instruction creation methods: Other Instructions
728 //===--------------------------------------------------------------------===//
730 Value *CreateSelect(Value *C, Value *True, Value *False,
731 const char *Name = "") {
732 if (Constant *CC = dyn_cast<Constant>(C))
733 if (Constant *TC = dyn_cast<Constant>(True))
734 if (Constant *FC = dyn_cast<Constant>(False))
735 return ConstantExpr::getSelect(CC, TC, FC);
736 return LLVMBuilder::CreateSelect(C, True, False, Name);
739 Value *CreateExtractElement(Value *Vec, Value *Idx,
740 const char *Name = "") {
741 if (Constant *VC = dyn_cast<Constant>(Vec))
742 if (Constant *IC = dyn_cast<Constant>(Idx))
743 return ConstantExpr::getExtractElement(VC, IC);
744 return LLVMBuilder::CreateExtractElement(Vec, Idx, Name);
747 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
748 const char *Name = "") {
749 if (Constant *VC = dyn_cast<Constant>(Vec))
750 if (Constant *NC = dyn_cast<Constant>(NewElt))
751 if (Constant *IC = dyn_cast<Constant>(Idx))
752 return ConstantExpr::getInsertElement(VC, NC, IC);
753 return LLVMBuilder::CreateInsertElement(Vec, NewElt, Idx, Name);
756 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
757 const char *Name = "") {
758 if (Constant *V1C = dyn_cast<Constant>(V1))
759 if (Constant *V2C = dyn_cast<Constant>(V2))
760 if (Constant *MC = dyn_cast<Constant>(Mask))
761 return ConstantExpr::getShuffleVector(V1C, V2C, MC);
762 return LLVMBuilder::CreateShuffleVector(V1, V2, Mask, Name);