llvm.org GIT mirror llvm / eed707b
Revert the ConstantInt constructors back to their 2.5 forms where possible, thanks to contexts-on-types. More to come. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77011 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 10 years ago
67 changed file(s) with 961 addition(s) and 1255 deletion(s). Raw diff Collapse all Expand all
7676 builder = new IRBuilder<>(BasicBlock::Create(label, brainf_func));
7777
7878 //%arr = malloc i8, i32 %d
79 ConstantInt *val_mem = C.getConstantInt(APInt(32, memtotal));
79 ConstantInt *val_mem = ConstantInt::get(C, APInt(32, memtotal));
8080 ptr_arr = builder->CreateMalloc(IntegerType::Int8Ty, val_mem, "arr");
8181
8282 //call void @llvm.memset.i32(i8 *%arr, i8 0, i32 %d, i32 1)
8383 {
8484 Value *memset_params[] = {
8585 ptr_arr,
86 C.getConstantInt(APInt(8, 0)),
86 ConstantInt::get(C, APInt(8, 0)),
8787 val_mem,
88 C.getConstantInt(APInt(32, 1))
88 ConstantInt::get(C, APInt(32, 1))
8989 };
9090
9191 CallInst *memset_call = builder->
9696 //%arrmax = getelementptr i8 *%arr, i32 %d
9797 if (comflag & flag_arraybounds) {
9898 ptr_arrmax = builder->
99 CreateGEP(ptr_arr, C.getConstantInt(APInt(32, memtotal)), "arrmax");
99 CreateGEP(ptr_arr, ConstantInt::get(C, APInt(32, memtotal)), "arrmax");
100100 }
101101
102102 //%head.%d = getelementptr i8 *%arr, i32 %d
103103 curhead = builder->CreateGEP(ptr_arr,
104 C.getConstantInt(APInt(32, memtotal/2)),
104 ConstantInt::get(C, APInt(32, memtotal/2)),
105105 headreg);
106106
107107
228228 {
229229 //%head.%d = getelementptr i8 *%head.%d, i32 %d
230230 curhead = builder->
231 CreateGEP(curhead, C.getConstantInt(APInt(32, curvalue)),
231 CreateGEP(curhead, ConstantInt::get(C, APInt(32, curvalue)),
232232 headreg);
233233
234234 //Error block for array out of bounds
263263
264264 //%tape.%d = add i8 %tape.%d, %d
265265 Value *tape_1 = builder->
266 CreateAdd(tape_0, C.getConstantInt(APInt(8, curvalue)), tapereg);
266 CreateAdd(tape_0, ConstantInt::get(C, APInt(8, curvalue)), tapereg);
267267
268268 //store i8 %tape.%d, i8 *%head.%d\n"
269269 builder->CreateStore(tape_1, curhead);
428428
429429 //%test.%d = icmp eq i8 %tape.%d, 0
430430 ICmpInst *test_0 = new ICmpInst(*testbb, ICmpInst::ICMP_EQ, tape_0,
431 C.getConstantInt(APInt(8, 0)), testreg);
431 ConstantInt::get(C, APInt(8, 0)), testreg);
432432
433433 //br i1 %test.%d, label %main.%d, label %main.%d
434434 BasicBlock *bb_0 = BasicBlock::Create(label, brainf_func);
7979 }
8080
8181 //ret i32 0
82 ReturnInst::Create(getGlobalContext().getConstantInt(APInt(32, 0)), bb);
82 ReturnInst::Create(ConstantInt::get(getGlobalContext(), APInt(32, 0)), bb);
8383 }
8484
8585 int main(int argc, char **argv) {
4646 BasicBlock *BB = BasicBlock::Create("EntryBlock", FibF);
4747
4848 // Get pointers to the constants.
49 Value *One = Context.getConstantInt(Type::Int32Ty, 1);
50 Value *Two = Context.getConstantInt(Type::Int32Ty, 2);
49 Value *One = ConstantInt::get(Type::Int32Ty, 1);
50 Value *Two = ConstantInt::get(Type::Int32Ty, 2);
5151
5252 // Get pointer to the integer argument of the add1 function...
5353 Argument *ArgX = FibF->arg_begin(); // Get the arg.
6868 BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F);
6969
7070 // Get pointers to the constant `1'.
71 Value *One = Context.getConstantInt(Type::Int32Ty, 1);
71 Value *One = ConstantInt::get(Type::Int32Ty, 1);
7272
7373 // Get pointers to the integer argument of the add1 function...
7474 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
9393 BB = BasicBlock::Create("EntryBlock", FooF);
9494
9595 // Get pointers to the constant `10'.
96 Value *Ten = Context.getConstantInt(Type::Int32Ty, 10);
96 Value *Ten = ConstantInt::get(Type::Int32Ty, 10);
9797
9898 // Pass Ten to the call call:
9999 CallInst *Add1CallRes = CallInst::Create(Add1F, Ten, "add1", BB);
4141 BasicBlock *BB = BasicBlock::Create("EntryBlock", F);
4242
4343 // Get pointers to the constant integers...
44 Value *Two = Context.getConstantInt(Type::Int32Ty, 2);
45 Value *Three = Context.getConstantInt(Type::Int32Ty, 3);
44 Value *Two = ConstantInt::get(Type::Int32Ty, 2);
45 Value *Three = ConstantInt::get(Type::Int32Ty, 3);
4646
4747 // Create the add instruction... does not insert...
4848 Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
4343 BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F);
4444
4545 // Get pointers to the constant `1'.
46 Value *One = M->getContext().getConstantInt(Type::Int32Ty, 1);
46 Value *One = ConstantInt::get(Type::Int32Ty, 1);
4747
4848 // Get pointers to the integer argument of the add1 function...
4949 assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
7171 BasicBlock *BB = BasicBlock::Create("EntryBlock", FibF);
7272
7373 // Get pointers to the constants.
74 Value *One = M->getContext().getConstantInt(Type::Int32Ty, 1);
75 Value *Two = M->getContext().getConstantInt(Type::Int32Ty, 2);
74 Value *One = ConstantInt::get(Type::Int32Ty, 1);
75 Value *Two = ConstantInt::get(Type::Int32Ty, 2);
7676
7777 // Get pointer to the integer argument of the add1 function...
7878 Argument *ArgX = FibF->arg_begin(); // Get the arg.
5656 return User::operator new(s, 0);
5757 }
5858 public:
59 /// If Ty is a vector type, return a Constant with a splat of the given
60 /// value. Otherwise return a ConstantInt for the given value.
61 static Constant* get(const Type* Ty, uint64_t V, bool isSigned = false);
62
63 /// Return a ConstantInt with the specified integer value for the specified
64 /// type. If the type is wider than 64 bits, the value will be zero-extended
65 /// to fit the type, unless isSigned is true, in which case the value will
66 /// be interpreted as a 64-bit signed integer and sign-extended to fit
67 /// the type.
68 /// @brief Get a ConstantInt for a specific value.
69 static ConstantInt* get(const IntegerType* Ty, uint64_t V,
70 bool isSigned = false);
71
72 /// Return a ConstantInt with the specified value for the specified type. The
73 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
74 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
75 /// signed value for the type Ty.
76 /// @brief Get a ConstantInt for a specific signed value.
77 static ConstantInt* getSigned(const IntegerType* Ty, int64_t V);
78 static Constant *getSigned(const Type *Ty, int64_t V);
79
80 /// Return a ConstantInt with the specified value and an implied Type. The
81 /// type is the integer type that corresponds to the bit width of the value.
82 static ConstantInt* get(LLVMContext &Context, const APInt& V);
83
84 /// If Ty is a vector type, return a Constant with a splat of the given
85 /// value. Otherwise return a ConstantInt for the given value.
86 static Constant* get(const Type* Ty, const APInt& V);
87
5988 /// Return the constant as an APInt value reference. This allows clients to
6089 /// obtain a copy of the value, with all its precision in tact.
6190 /// @brief Return the constant's value.
5252 /// to have one context per thread.
5353 class LLVMContext {
5454 LLVMContextImpl* pImpl;
55
56 friend class ConstantInt;
5557 public:
5658 LLVMContext();
5759 ~LLVMContext();
7072 // ConstantInt accessors
7173 ConstantInt* getTrue();
7274 ConstantInt* getFalse();
73
74 /// If Ty is a vector type, return a Constant with a splat of the given
75 /// value. Otherwise return a ConstantInt for the given value.
76 Constant* getConstantInt(const Type* Ty, uint64_t V,
77 bool isSigned = false);
78
79 /// Return a ConstantInt with the specified integer value for the specified
80 /// type. If the type is wider than 64 bits, the value will be zero-extended
81 /// to fit the type, unless isSigned is true, in which case the value will
82 /// be interpreted as a 64-bit signed integer and sign-extended to fit
83 /// the type.
84 /// @brief Get a ConstantInt for a specific value.
85 ConstantInt* getConstantInt(const IntegerType* Ty, uint64_t V,
86 bool isSigned = false);
87
88 /// Return a ConstantInt with the specified value for the specified type. The
89 /// value V will be canonicalized to a an unsigned APInt. Accessing it with
90 /// either getSExtValue() or getZExtValue() will yield a correctly sized and
91 /// signed value for the type Ty.
92 /// @brief Get a ConstantInt for a specific signed value.
93 ConstantInt* getConstantIntSigned(const IntegerType* Ty, int64_t V);
94 Constant *getConstantIntSigned(const Type *Ty, int64_t V);
95
96 /// Return a ConstantInt with the specified value and an implied Type. The
97 /// type is the integer type that corresponds to the bit width of the value.
98 ConstantInt* getConstantInt(const APInt& V);
99
100 /// If Ty is a vector type, return a Constant with a splat of the given
101 /// value. Otherwise return a ConstantInt for the given value.
102 Constant* getConstantInt(const Type* Ty, const APInt& V);
10375
10476 // ConstantPointerNull accessors
10577 ConstantPointerNull* getConstantPointerNull(const PointerType* T);
369369 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
370370 }
371371 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") {
372 Value *Idx = Context.getConstantInt(Type::Int32Ty, Idx0);
372 Value *Idx = ConstantInt::get(Type::Int32Ty, Idx0);
373373
374374 if (Constant *PC = dyn_cast(Ptr))
375375 return Folder.CreateGetElementPtr(PC, &Idx, 1);
379379 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
380380 const char *Name = "") {
381381 Value *Idxs[] = {
382 Context.getConstantInt(Type::Int32Ty, Idx0),
383 Context.getConstantInt(Type::Int32Ty, Idx1)
382 ConstantInt::get(Type::Int32Ty, Idx0),
383 ConstantInt::get(Type::Int32Ty, Idx1)
384384 };
385385
386386 if (Constant *PC = dyn_cast(Ptr))
389389 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
390390 }
391391 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") {
392 Value *Idx = Context.getConstantInt(Type::Int64Ty, Idx0);
392 Value *Idx = ConstantInt::get(Type::Int64Ty, Idx0);
393393
394394 if (Constant *PC = dyn_cast(Ptr))
395395 return Folder.CreateGetElementPtr(PC, &Idx, 1);
399399 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
400400 const char *Name = "") {
401401 Value *Idxs[] = {
402 Context.getConstantInt(Type::Int64Ty, Idx0),
403 Context.getConstantInt(Type::Int64Ty, Idx1)
402 ConstantInt::get(Type::Int64Ty, Idx0),
403 ConstantInt::get(Type::Int64Ty, Idx1)
404404 };
405405
406406 if (Constant *PC = dyn_cast(Ptr))
427427 }
428428 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
429429 Value *gv = CreateGlobalString(Str, Name);
430 Value *zero = Context.getConstantInt(Type::Int32Ty, 0);
430 Value *zero = ConstantInt::get(Type::Int32Ty, 0);
431431 Value *Args[] = { zero, zero };
432432 return CreateGEP(gv, Args, Args+2, Name);
433433 }
779779 //
780780 if (const ArrayType *AT = dyn_cast(BasePtr1Ty))
781781 GEP1Ops[i] =
782 Context.getConstantInt(Type::Int64Ty,AT->getNumElements()-1);
782 ConstantInt::get(Type::Int64Ty,AT->getNumElements()-1);
783783 else if (const VectorType *VT = dyn_cast(BasePtr1Ty))
784784 GEP1Ops[i] =
785 Context.getConstantInt(Type::Int64Ty,VT->getNumElements()-1);
785 ConstantInt::get(Type::Int64Ty,VT->getNumElements()-1);
786786 }
787787 }
788788
112112 if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *TD) &&
113113 GV1 == GV2) {
114114 // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow.
115 return Context.getConstantInt(Op0->getType(), Offs1-Offs2);
115 return ConstantInt::get(Op0->getType(), Offs1-Offs2);
116116 }
117117 }
118118
150150
151151 uint64_t Offset = TD->getIndexedOffset(Ptr->getType(),
152152 (Value**)Ops+1, NumOps-1);
153 Constant *C = Context.getConstantInt(TD->getIntPtrType(), Offset+BasePtr);
153 Constant *C = ConstantInt::get(TD->getIntPtrType(), Offset+BasePtr);
154154 return Context.getConstantExprIntToPtr(C, ResultTy);
155155 }
156156
231231
232232 // Shift it to the right place, depending on endianness.
233233 Src = Context.getConstantExprShl(Src,
234 Context.getConstantInt(Src->getType(), ShiftAmt));
234 ConstantInt::get(Src->getType(), ShiftAmt));
235235 ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
236236
237237 // Mix it in.
254254 // Shift the piece of the value into the right place, depending on
255255 // endianness.
256256 Constant *Elt = Context.getConstantExprLShr(Src,
257 Context.getConstantInt(Src->getType(), ShiftAmt));
257 ConstantInt::get(Src->getType(), ShiftAmt));
258258 ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
259259
260260 // Truncate and remember this piece.
375375 unsigned InWidth = Input->getType()->getScalarSizeInBits();
376376 if (TD->getPointerSizeInBits() < InWidth) {
377377 Constant *Mask =
378 Context.getConstantInt(APInt::getLowBitsSet(InWidth,
378 ConstantInt::get(Context, APInt::getLowBitsSet(InWidth,
379379 TD->getPointerSizeInBits()));
380380 Input = Context.getConstantExprAnd(Input, Mask);
381381 }
419419 AT->getNumElements()))) {
420420 Constant *Index[] = {
421421 Context.getNullValue(CE->getType()),
422 Context.getConstantInt(ElemIdx)
422 ConstantInt::get(Context, ElemIdx)
423423 };
424424 return
425425 Context.getConstantExprGetElementPtr(GV, &Index[0], 2);
800800 }
801801 } else if (ConstantInt *Op = dyn_cast(Operands[0])) {
802802 if (Len > 11 && !memcmp(Str, "llvm.bswap", 10))
803 return Context.getConstantInt(Op->getValue().byteSwap());
803 return ConstantInt::get(Context, Op->getValue().byteSwap());
804804 else if (Len > 11 && !memcmp(Str, "llvm.ctpop", 10))
805 return Context.getConstantInt(Ty, Op->getValue().countPopulation());
805 return ConstantInt::get(Ty, Op->getValue().countPopulation());
806806 else if (Len > 10 && !memcmp(Str, "llvm.cttz", 9))
807 return Context.getConstantInt(Ty, Op->getValue().countTrailingZeros());
807 return ConstantInt::get(Ty, Op->getValue().countTrailingZeros());
808808 else if (Len > 10 && !memcmp(Str, "llvm.ctlz", 9))
809 return Context.getConstantInt(Ty, Op->getValue().countLeadingZeros());
809 return ConstantInt::get(Ty, Op->getValue().countLeadingZeros());
810810 }
811811 } else if (NumOperands == 2) {
812812 if (ConstantFP *Op1 = dyn_cast(Operands[0])) {
481481 Constant *DIFactory::GetTagConstant(unsigned TAG) {
482482 assert((TAG & LLVMDebugVersionMask) == 0 &&
483483 "Tag too large for debug encoding!");
484 return VMContext.getConstantInt(Type::Int32Ty, TAG | LLVMDebugVersion);
484 return ConstantInt::get(Type::Int32Ty, TAG | LLVMDebugVersion);
485485 }
486486
487487 Constant *DIFactory::GetStringConstant(const std::string &String) {
540540 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
541541 Constant *Elts[] = {
542542 GetTagConstant(dwarf::DW_TAG_subrange_type),
543 VMContext.getConstantInt(Type::Int64Ty, Lo),
544 VMContext.getConstantInt(Type::Int64Ty, Hi)
543 ConstantInt::get(Type::Int64Ty, Lo),
544 ConstantInt::get(Type::Int64Ty, Hi)
545545 };
546546
547547 Constant *Init = VMContext.getConstantStruct(Elts,
576576 Constant *Elts[] = {
577577 GetTagConstant(dwarf::DW_TAG_compile_unit),
578578 VMContext.getNullValue(EmptyStructPtr),
579 VMContext.getConstantInt(Type::Int32Ty, LangID),
579 ConstantInt::get(Type::Int32Ty, LangID),
580580 GetStringConstant(Filename),
581581 GetStringConstant(Directory),
582582 GetStringConstant(Producer),
583 VMContext.getConstantInt(Type::Int1Ty, isMain),
584 VMContext.getConstantInt(Type::Int1Ty, isOptimized),
583 ConstantInt::get(Type::Int1Ty, isMain),
584 ConstantInt::get(Type::Int1Ty, isOptimized),
585585 GetStringConstant(Flags),
586 VMContext.getConstantInt(Type::Int32Ty, RunTimeVer)
586 ConstantInt::get(Type::Int32Ty, RunTimeVer)
587587 };
588588
589589 Constant *Init = VMContext.getConstantStruct(Elts,
602602 Constant *Elts[] = {
603603 GetTagConstant(dwarf::DW_TAG_enumerator),
604604 GetStringConstant(Name),
605 VMContext.getConstantInt(Type::Int64Ty, Val)
605 ConstantInt::get(Type::Int64Ty, Val)
606606 };
607607
608608 Constant *Init = VMContext.getConstantStruct(Elts,
631631 getCastToEmpty(Context),
632632 GetStringConstant(Name),
633633 getCastToEmpty(CompileUnit),
634 VMContext.getConstantInt(Type::Int32Ty, LineNumber),
635 VMContext.getConstantInt(Type::Int64Ty, SizeInBits),
636 VMContext.getConstantInt(Type::Int64Ty, AlignInBits),
637 VMContext.getConstantInt(Type::Int64Ty, OffsetInBits),
638 VMContext.getConstantInt(Type::Int32Ty, Flags),
639 VMContext.getConstantInt(Type::Int32Ty, Encoding)
634 ConstantInt::get(Type::Int32Ty, LineNumber),
635 ConstantInt::get(Type::Int64Ty, SizeInBits),
636 ConstantInt::get(Type::Int64Ty, AlignInBits),
637 ConstantInt::get(Type::Int64Ty, OffsetInBits),
638 ConstantInt::get(Type::Int32Ty, Flags),
639 ConstantInt::get(Type::Int32Ty, Encoding)
640640 };
641641
642642 Constant *Init = VMContext.getConstantStruct(Elts,
667667 getCastToEmpty(Context),
668668 GetStringConstant(Name),
669669 getCastToEmpty(CompileUnit),
670 VMContext.getConstantInt(Type::Int32Ty, LineNumber),
671 VMContext.getConstantInt(Type::Int64Ty, SizeInBits),
672 VMContext.getConstantInt(Type::Int64Ty, AlignInBits),
673 VMContext.getConstantInt(Type::Int64Ty, OffsetInBits),
674 VMContext.getConstantInt(Type::Int32Ty, Flags),
670 ConstantInt::get(Type::Int32Ty, LineNumber),
671 ConstantInt::get(Type::Int64Ty, SizeInBits),
672 ConstantInt::get(Type::Int64Ty, AlignInBits),
673 ConstantInt::get(Type::Int64Ty, OffsetInBits),
674 ConstantInt::get(Type::Int32Ty, Flags),
675675 getCastToEmpty(DerivedFrom)
676676 };
677677
705705 getCastToEmpty(Context),
706706 GetStringConstant(Name),
707707 getCastToEmpty(CompileUnit),
708 VMContext.getConstantInt(Type::Int32Ty, LineNumber),
709 VMContext.getConstantInt(Type::Int64Ty, SizeInBits),
710 VMContext.getConstantInt(Type::Int64Ty, AlignInBits),
711 VMContext.getConstantInt(Type::Int64Ty, OffsetInBits),
712 VMContext.getConstantInt(Type::Int32Ty, Flags),
708 ConstantInt::get(Type::Int32Ty, LineNumber),
709 ConstantInt::get(Type::Int64Ty, SizeInBits),
710 ConstantInt::get(Type::Int64Ty, AlignInBits),
711 ConstantInt::get(Type::Int64Ty, OffsetInBits),
712 ConstantInt::get(Type::Int32Ty, Flags),
713713 getCastToEmpty(DerivedFrom),
714714 getCastToEmpty(Elements),
715 VMContext.getConstantInt(Type::Int32Ty, RuntimeLang)
715 ConstantInt::get(Type::Int32Ty, RuntimeLang)
716716 };
717717
718718 Constant *Init = VMContext.getConstantStruct(Elts,
747747 GetStringConstant(DisplayName),
748748 GetStringConstant(LinkageName),
749749 getCastToEmpty(CompileUnit),
750 VMContext.getConstantInt(Type::Int32Ty, LineNo),
750 ConstantInt::get(Type::Int32Ty, LineNo),
751751 getCastToEmpty(Type),
752 VMContext.getConstantInt(Type::Int1Ty, isLocalToUnit),
753 VMContext.getConstantInt(Type::Int1Ty, isDefinition)
752 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
753 ConstantInt::get(Type::Int1Ty, isDefinition)
754754 };
755755
756756 Constant *Init = VMContext.getConstantStruct(Elts,
780780 GetStringConstant(DisplayName),
781781 GetStringConstant(LinkageName),
782782 getCastToEmpty(CompileUnit),
783 VMContext.getConstantInt(Type::Int32Ty, LineNo),
783 ConstantInt::get(Type::Int32Ty, LineNo),
784784 getCastToEmpty(Type),
785 VMContext.getConstantInt(Type::Int1Ty, isLocalToUnit),
786 VMContext.getConstantInt(Type::Int1Ty, isDefinition),
785 ConstantInt::get(Type::Int1Ty, isLocalToUnit),
786 ConstantInt::get(Type::Int1Ty, isDefinition),
787787 VMContext.getConstantExprBitCast(Val, EmptyStructPtr)
788788 };
789789
809809 getCastToEmpty(Context),
810810 GetStringConstant(Name),
811811 getCastToEmpty(CompileUnit),
812 VMContext.getConstantInt(Type::Int32Ty, LineNo),
812 ConstantInt::get(Type::Int32Ty, LineNo),
813813 getCastToEmpty(Type)
814814 };
815815
861861
862862 // Invoke llvm.dbg.stoppoint
863863 Value *Args[] = {
864 VMContext.getConstantInt(llvm::Type::Int32Ty, LineNo),
865 VMContext.getConstantInt(llvm::Type::Int32Ty, ColNo),
864 ConstantInt::get(llvm::Type::Int32Ty, LineNo),
865 ConstantInt::get(llvm::Type::Int32Ty, ColNo),
866866 getCastToEmpty(CU)
867867 };
868868 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
191191 }
192192
193193 const SCEV *ScalarEvolution::getConstant(const APInt& Val) {
194 return getConstant(getContext().getConstantInt(Val));
194 return getConstant(ConstantInt::get(getContext(), Val));
195195 }
196196
197197 const SCEV *
198198 ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) {
199199 return getConstant(
200 getContext().getConstantInt(cast(Ty), V, isSigned));
200 ConstantInt::get(cast(Ty), V, isSigned));
201201 }
202202
203203 const Type *SCEVConstant::getType() const { return V->getType(); }
15171517 ++Idx;
15181518 while (const SCEVConstant *RHSC = dyn_cast(Ops[Idx])) {
15191519 // We found two constants, fold them together!
1520 ConstantInt *Fold = getContext().getConstantInt(LHSC->getValue()->getValue() *
1520 ConstantInt *Fold = ConstantInt::get(getContext(),
1521 LHSC->getValue()->getValue() *
15211522 RHSC->getValue()->getValue());
15221523 Ops[0] = getConstant(Fold);
15231524 Ops.erase(Ops.begin()+1); // Erase the folded element
18681869 assert(Idx < Ops.size());
18691870 while (const SCEVConstant *RHSC = dyn_cast(Ops[Idx])) {
18701871 // We found two constants, fold them together!
1871 ConstantInt *Fold = getContext().getConstantInt(
1872 ConstantInt *Fold = ConstantInt::get(getContext(),
18721873 APIntOps::smax(LHSC->getValue()->getValue(),
18731874 RHSC->getValue()->getValue()));
18741875 Ops[0] = getConstant(Fold);
19651966 assert(Idx < Ops.size());
19661967 while (const SCEVConstant *RHSC = dyn_cast(Ops[Idx])) {
19671968 // We found two constants, fold them together!
1968 ConstantInt *Fold = getContext().getConstantInt(
1969 ConstantInt *Fold = ConstantInt::get(getContext(),
19691970 APIntOps::umax(LHSC->getValue()->getValue(),
19701971 RHSC->getValue()->getValue()));
19711972 Ops[0] = getConstant(Fold);
21322133 /// specified signed integer value and return a SCEV for the constant.
21332134 const SCEV *ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) {
21342135 const IntegerType *ITy = cast(getEffectiveSCEVType(Ty));
2135 return getConstant(getContext().getConstantInt(ITy, Val));
2136 return getConstant(ConstantInt::get(ITy, Val));
21362137 }
21372138
21382139 /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
28952896 // Turn shift left of a constant amount into a multiply.
28962897 if (ConstantInt *SA = dyn_cast(U->getOperand(1))) {
28972898 uint32_t BitWidth = cast(V->getType())->getBitWidth();
2898 Constant *X = getContext().getConstantInt(
2899 Constant *X = ConstantInt::get(getContext(),
28992900 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
29002901 return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X));
29012902 }
29052906 // Turn logical shift right of a constant into a unsigned divide.
29062907 if (ConstantInt *SA = dyn_cast(U->getOperand(1))) {
29072908 uint32_t BitWidth = cast(V->getType())->getBitWidth();
2908 Constant *X = getContext().getConstantInt(
2909 Constant *X = ConstantInt::get(getContext(),
29092910 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
29102911 return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X));
29112912 }
35573558
35583559 unsigned MaxSteps = MaxBruteForceIterations;
35593560 for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
3560 ConstantInt *ItCst = getContext().getConstantInt(
3561 ConstantInt *ItCst = ConstantInt::get(
35613562 cast(IdxExpr->getType()), IterationNum);
35623563 ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this);
35633564
40714072 LLVMContext &Context = SE.getContext();
40724073
40734074 ConstantInt *Solution1 =
4074 Context.getConstantInt((NegB + SqrtVal).sdiv(TwoA));
4075 ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA));
40754076 ConstantInt *Solution2 =
4076 Context.getConstantInt((NegB - SqrtVal).sdiv(TwoA));
4077 ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA));
40774078
40784079 return std::make_pair(SE.getConstant(Solution1),
40794080 SE.getConstant(Solution2));
48354836
48364837 // The exit value should be (End+A)/A.
48374838 APInt ExitVal = (End + A).udiv(A);
4838 ConstantInt *ExitValue = SE.getContext().getConstantInt(ExitVal);
4839 ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal);
48394840
48404841 // Evaluate at the exit value. If we really did fall out of the valid
48414842 // range, then we computed our trip count, otherwise wrap around or other
48474848 // Ensure that the previous value is in the range. This is a sanity check.
48484849 assert(Range.contains(
48494850 EvaluateConstantChrecAtConstant(this,
4850 SE.getContext().getConstantInt(ExitVal - One), SE)->getValue()) &&
4851 ConstantInt::get(SE.getContext(), ExitVal - One), SE)->getValue()) &&
48514852 "Linear scev computation is off in a bad way!");
48524853 return SE.getConstant(ExitValue);
48534854 } else if (isQuadratic()) {
48824883 if (Range.contains(R1Val->getValue())) {
48834884 // The next iteration must be out of the range...
48844885 ConstantInt *NextVal =
4885 SE.getContext().getConstantInt(R1->getValue()->getValue()+1);
4886 ConstantInt::get(SE.getContext(), R1->getValue()->getValue()+1);
48864887
48874888 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
48884889 if (!Range.contains(R1Val->getValue()))
48934894 // If R1 was not in the range, then it is a good return value. Make
48944895 // sure that R1-1 WAS in the range though, just in case.
48954896 ConstantInt *NextVal =
4896 SE.getContext().getConstantInt(R1->getValue()->getValue()-1);
4897 ConstantInt::get(SE.getContext(), R1->getValue()->getValue()-1);
48974898 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
48984899 if (Range.contains(R1Val->getValue()))
48994900 return R1;
166166 // For a Constant, check for a multiple of the given factor.
167167 if (const SCEVConstant *C = dyn_cast(S)) {
168168 ConstantInt *CI =
169 SE.getContext().getConstantInt(C->getValue()->getValue().sdiv(Factor));
169 ConstantInt::get(SE.getContext(), C->getValue()->getValue().sdiv(Factor));
170170 // If the quotient is zero and the remainder is non-zero, reject
171171 // the value at this scale. It will be considered for subsequent
172172 // smaller scales.
297297 uint64_t FullOffset = C->getValue()->getZExtValue();
298298 if (FullOffset < SL.getSizeInBytes()) {
299299 unsigned ElIdx = SL.getElementContainingOffset(FullOffset);
300 GepIndices.push_back(
301 getContext().getConstantInt(Type::Int32Ty, ElIdx));
300 GepIndices.push_back(ConstantInt::get(Type::Int32Ty, ElIdx));
302301 ElTy = STy->getTypeAtIndex(ElIdx);
303302 Ops[0] =
304303 SE.getConstant(Ty, FullOffset - SL.getElementOffset(ElIdx));
411410 const APInt &RHS = SC->getValue()->getValue();
412411 if (RHS.isPowerOf2())
413412 return InsertBinop(Instruction::LShr, LHS,
414 getContext().getConstantInt(Ty, RHS.logBase2()));
413 ConstantInt::get(Ty, RHS.logBase2()));
415414 }
416415
417416 Value *RHS = expandCodeFor(S->getRHS(), Ty);
531530
532531 // Insert a unit add instruction right before the terminator corresponding
533532 // to the back-edge.
534 Constant *One = getContext().getConstantInt(Ty, 1);
533 Constant *One = ConstantInt::get(Ty, 1);
535534 Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next",
536535 (*HPI)->getTerminator());
537536 InsertedValues.insert(Add);
21372137 if (!isa(Ty))
21382138 return Error(ID.Loc, "integer constant must have integer type");
21392139 ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
2140 V = Context.getConstantInt(ID.APSIntVal);
2140 V = ConstantInt::get(Context, ID.APSIntVal);
21412141 return false;
21422142 case ValID::t_APFloat:
21432143 if (!Ty->isFloatingPoint() ||
876876 case bitc::CST_CODE_INTEGER: // INTEGER: [intval]
877877 if (!isa(CurTy) || Record.empty())
878878 return Error("Invalid CST_INTEGER record");
879 V = Context.getConstantInt(CurTy, DecodeSignRotatedValue(Record[0]));
879 V = ConstantInt::get(CurTy, DecodeSignRotatedValue(Record[0]));
880880 break;
881881 case bitc::CST_CODE_WIDE_INTEGER: {// WIDE_INTEGER: [n x intval]
882882 if (!isa(CurTy) || Record.empty())
887887 Words.resize(NumWords);
888888 for (unsigned i = 0; i != NumWords; ++i)
889889 Words[i] = DecodeSignRotatedValue(Record[i]);
890 V = Context.getConstantInt(APInt(cast(CurTy)->getBitWidth(),
891 NumWords, &Words[0]));
890 V = ConstantInt::get(Context,
891 APInt(cast(CurTy)->getBitWidth(),
892 NumWords, &Words[0]));
892893 break;
893894 }
894895 case bitc::CST_CODE_FLOAT: { // FLOAT: [fpval]
950951 unsigned Size = Record.size();
951952 std::vector Elts;
952953 for (unsigned i = 0; i != Size; ++i)
953 Elts.push_back(Context.getConstantInt(EltTy, Record[i]));
954 Elts.push_back(ConstantInt::get(EltTy, Record[i]));
954955 V = Context.getConstantArray(ATy, Elts);
955956 break;
956957 }
964965 unsigned Size = Record.size();
965966 std::vector Elts;
966967 for (unsigned i = 0; i != Size; ++i)
967 Elts.push_back(Context.getConstantInt(EltTy, Record[i]));
968 Elts.push_back(ConstantInt::get(EltTy, Record[i]));
968969 Elts.push_back(Context.getNullValue(EltTy));
969970 V = Context.getConstantArray(ATy, Elts);
970971 break;
158158 switch(BitSize) {
159159 default: llvm_unreachable("Unhandled type size of value to byteswap!");
160160 case 16: {
161 Value *Tmp1 = Builder.CreateShl(V, Context.getConstantInt(V->getType(), 8),
161 Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
162162 "bswap.2");
163 Value *Tmp2 = Builder.CreateLShr(V, Context.getConstantInt(V->getType(), 8),
163 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
164164 "bswap.1");
165165 V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16");
166166 break;
167167 }
168168 case 32: {
169 Value *Tmp4 = Builder.CreateShl(V, Context.getConstantInt(V->getType(), 24),
169 Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
170170 "bswap.4");
171 Value *Tmp3 = Builder.CreateShl(V, Context.getConstantInt(V->getType(), 8),
171 Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
172172 "bswap.3");
173 Value *Tmp2 = Builder.CreateLShr(V, Context.getConstantInt(V->getType(), 8),
173 Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
174174 "bswap.2");
175 Value *Tmp1 = Builder.CreateLShr(V,Context.getConstantInt(V->getType(), 24),
175 Value *Tmp1 = Builder.CreateLShr(V,ConstantInt::get(V->getType(), 24),
176176 "bswap.1");
177177 Tmp3 = Builder.CreateAnd(Tmp3,
178 Context.getConstantInt(Type::Int32Ty, 0xFF0000),
178 ConstantInt::get(Type::Int32Ty, 0xFF0000),
179179 "bswap.and3");
180180 Tmp2 = Builder.CreateAnd(Tmp2,
181 Context.getConstantInt(Type::Int32Ty, 0xFF00),
181 ConstantInt::get(Type::Int32Ty, 0xFF00),
182182 "bswap.and2");
183183 Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1");
184184 Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2");
186186 break;
187187 }
188188 case 64: {
189 Value *Tmp8 = Builder.CreateShl(V, Context.getConstantInt(V->getType(), 56),
189 Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56),
190190 "bswap.8");
191 Value *Tmp7 = Builder.CreateShl(V, Context.getConstantInt(V->getType(), 40),
191 Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40),
192192 "bswap.7");
193 Value *Tmp6 = Builder.CreateShl(V, Context.getConstantInt(V->getType(), 24),
193 Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24),
194194 "bswap.6");
195 Value *Tmp5 = Builder.CreateShl(V, Context.getConstantInt(V->getType(), 8),
195 Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8),
196196 "bswap.5");
197 Value* Tmp4 = Builder.CreateLShr(V, Context.getConstantInt(V->getType(), 8),
197 Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8),
198198 "bswap.4");
199199 Value* Tmp3 = Builder.CreateLShr(V,
200 Context.getConstantInt(V->getType(), 24),
200 ConstantInt::get(V->getType(), 24),
201201 "bswap.3");
202202 Value* Tmp2 = Builder.CreateLShr(V,
203 Context.getConstantInt(V->getType(), 40),
203 ConstantInt::get(V->getType(), 40),
204204 "bswap.2");
205205 Value* Tmp1 = Builder.CreateLShr(V,
206 Context.getConstantInt(V->getType(), 56),
206 ConstantInt::get(V->getType(), 56),
207207 "bswap.1");
208208 Tmp7 = Builder.CreateAnd(Tmp7,
209 Context.getConstantInt(Type::Int64Ty,
209 ConstantInt::get(Type::Int64Ty,
210210 0xFF000000000000ULL),
211211 "bswap.and7");
212212 Tmp6 = Builder.CreateAnd(Tmp6,
213 Context.getConstantInt(Type::Int64Ty,
213 ConstantInt::get(Type::Int64Ty,
214214 0xFF0000000000ULL),
215215 "bswap.and6");
216216 Tmp5 = Builder.CreateAnd(Tmp5,
217 Context.getConstantInt(Type::Int64Ty, 0xFF00000000ULL),
217 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
218218 "bswap.and5");
219219 Tmp4 = Builder.CreateAnd(Tmp4,
220 Context.getConstantInt(Type::Int64Ty, 0xFF000000ULL),
220 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
221221 "bswap.and4");
222222 Tmp3 = Builder.CreateAnd(Tmp3,
223 Context.getConstantInt(Type::Int64Ty, 0xFF0000ULL),
223 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
224224 "bswap.and3");
225225 Tmp2 = Builder.CreateAnd(Tmp2,
226 Context.getConstantInt(Type::Int64Ty, 0xFF00ULL),
226 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
227227 "bswap.and2");
228228 Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1");
229229 Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2");
253253
254254 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
255255 unsigned WordSize = (BitSize + 63) / 64;
256 Value *Count = Context.getConstantInt(V->getType(), 0);
256 Value *Count = ConstantInt::get(V->getType(), 0);
257257
258258 for (unsigned n = 0; n < WordSize; ++n) {
259259 Value *PartValue = V;
260260 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
261261 i <<= 1, ++ct) {
262 Value *MaskCst = Context.getConstantInt(V->getType(), MaskValues[ct]);
262 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
263263 Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1");
264264 Value *VShift = Builder.CreateLShr(PartValue,
265 Context.getConstantInt(V->getType(), i),
265 ConstantInt::get(V->getType(), i),
266266 "ctpop.sh");
267267 Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2");
268268 PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step");
269269 }
270270 Count = Builder.CreateAdd(PartValue, Count, "ctpop.part");
271271 if (BitSize > 64) {
272 V = Builder.CreateLShr(V, Context.getConstantInt(V->getType(), 64),
272 V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64),
273273 "ctpop.part.sh");
274274 BitSize -= 64;
275275 }
286286
287287 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
288288 for (unsigned i = 1; i < BitSize; i <<= 1) {
289 Value *ShVal = Context.getConstantInt(V->getType(), i);
289 Value *ShVal = ConstantInt::get(V->getType(), i);
290290 ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh");
291291 V = Builder.CreateOr(V, ShVal, "ctlz.step");
292292 }
377377 Value *Src = CI->getOperand(1);
378378 Value *NotSrc = Builder.CreateNot(Src);
379379 NotSrc->setName(Src->getName() + ".not");
380 Value *SrcM1 = Context.getConstantInt(Src->getType(), 1);
380 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
381381 SrcM1 = Builder.CreateSub(Src, SrcM1);
382382 Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI);
383383 CI->replaceAllUsesWith(Src);
413413 case Intrinsic::readcyclecounter: {
414414 cerr << "WARNING: this target does not support the llvm.readcyclecoun"
415415 << "ter intrinsic. It is being lowered to a constant 0\n";
416 CI->replaceAllUsesWith(Context.getConstantInt(Type::Int64Ty, 0));
416 CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0));
417417 break;
418418 }
419419
433433 case Intrinsic::eh_typeid_for_i32:
434434 case Intrinsic::eh_typeid_for_i64:
435435 // Return something different to eh_selector.
436 CI->replaceAllUsesWith(Context.getConstantInt(CI->getType(), 1));
436 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
437437 break;
438438
439439 case Intrinsic::var_annotation:
505505 case Intrinsic::flt_rounds:
506506 // Lower to "round to the nearest"
507507 if (CI->getType() != Type::VoidTy)
508 CI->replaceAllUsesWith(Context.getConstantInt(CI->getType(), 1));
508 CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1));
509509 break;
510510 }
511511
108108 APInt IntVal(IntBitWidth, 2, x);
109109
110110 unsigned IntegerReg =
111 getRegForValue(V->getContext().getConstantInt(IntVal));
111 getRegForValue(ConstantInt::get(V->getContext(), IntVal));
112112 if (IntegerReg != 0)
113113 Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
114114 }
20372037 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
20382038 }
20392039 if (TLI.isLittleEndian()) FF <<= 32;
2040 Constant *FudgeFactor = DAG.getContext()->getConstantInt(Type::Int64Ty, FF);
2040 Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
20412041
20422042 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
20432043 unsigned Alignment = cast(CPIdx)->getAlignment();
23052305
23062306 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
23072307 SDValue FudgePtr = DAG.getConstantPool(
2308 DAG.getContext()->getConstantInt(FF.zext(64)),
2308 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
23092309 TLI.getPointerTy());
23102310
23112311 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
873873 }
874874
875875 SDValue SelectionDAG::getConstant(const APInt &Val, MVT VT, bool isT) {
876 return getConstant(*Context->getConstantInt(Val), VT, isT);
876 return getConstant(*ConstantInt::get(*Context, Val), VT, isT);
877877 }
878878
879879 SDValue SelectionDAG::getConstant(const ConstantInt &Val, MVT VT, bool isT) {
202202 }
203203
204204 Constant *BaseElts[] = {
205 Context.getConstantInt(Type::Int32Ty, Roots.size(), false),
206 Context.getConstantInt(Type::Int32Ty, NumMeta, false),
205 ConstantInt::get(Type::Int32Ty, Roots.size(), false),
206 ConstantInt::get(Type::Int32Ty, NumMeta, false),
207207 };
208208
209209 Constant *DescriptorElts[] = {
235235 GlobalVariable::InternalLinkage,
236236 FrameMap, "__gc_" + F.getName());
237237
238 Constant *GEPIndices[2] = { Context.getConstantInt(Type::Int32Ty, 0),
239 Context.getConstantInt(Type::Int32Ty, 0) };
238 Constant *GEPIndices[2] = { ConstantInt::get(Type::Int32Ty, 0),
239 ConstantInt::get(Type::Int32Ty, 0) };
240240 return Context.getConstantExprGetElementPtr(GV, GEPIndices, 2);
241241 }
242242
341341 GetElementPtrInst *
342342 ShadowStackGC::CreateGEP(LLVMContext &Context, IRBuilder<> &B, Value *BasePtr,
343343 int Idx, int Idx2, const char *Name) {
344 Value *Indices[] = { Context.getConstantInt(Type::Int32Ty, 0),
345 Context.getConstantInt(Type::Int32Ty, Idx),
346 Context.getConstantInt(Type::Int32Ty, Idx2) };
344 Value *Indices[] = { ConstantInt::get(Type::Int32Ty, 0),
345 ConstantInt::get(Type::Int32Ty, Idx),
346 ConstantInt::get(Type::Int32Ty, Idx2) };
347347 Value* Val = B.CreateGEP(BasePtr, Indices, Indices + 3, Name);
348348
349349 assert(isa(Val) && "Unexpected folded constant");
354354 GetElementPtrInst *
355355 ShadowStackGC::CreateGEP(LLVMContext &Context, IRBuilder<> &B, Value *BasePtr,
356356 int Idx, const char *Name) {
357 Value *Indices[] = { Context.getConstantInt(Type::Int32Ty, 0),
358 Context.getConstantInt(Type::Int32Ty, Idx) };
357 Value *Indices[] = { ConstantInt::get(Type::Int32Ty, 0),
358 ConstantInt::get(Type::Int32Ty, Idx) };
359359 Value *Val = B.CreateGEP(BasePtr, Indices, Indices + 2, Name);
360360
361361 assert(isa(Val) && "Unexpected folded constant");
486486 switch (ArgTy->getTypeID()) {
487487 default: llvm_unreachable("Unknown argument type for function call!");
488488 case Type::IntegerTyID:
489 C = Context.getConstantInt(AV.IntVal);
489 C = ConstantInt::get(F->getContext(), AV.IntVal);
490490 break;
491491 case Type::FloatTyID:
492492 C = Context.getConstantFP(APFloat(AV.FloatVal));
502502 case Type::PointerTyID:
503503 void *ArgPtr = GVTOP(AV);
504504 if (sizeof(void*) == 4)
505 C = Context.getConstantInt(Type::Int32Ty, (int)(intptr_t)ArgPtr);
505 C = ConstantInt::get(Type::Int32Ty, (int)(intptr_t)ArgPtr);
506506 else
507 C = Context.getConstantInt(Type::Int64Ty, (intptr_t)ArgPtr);
507 C = ConstantInt::get(Type::Int64Ty, (intptr_t)ArgPtr);
508508 // Cast the integer to pointer
509509 C = Context.getConstantExprIntToPtr(C, ArgTy);
510510 break;
902902 unsigned PredReg) const {
903903 MachineFunction &MF = *MBB.getParent();
904904 MachineConstantPool *ConstantPool = MF.getConstantPool();
905 Constant *C =
906 MF.getFunction()->getContext().getConstantInt(Type::Int32Ty, Val);
905 Constant *C = ConstantInt::get(Type::Int32Ty, Val);
907906 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
908907
909908 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
876876 !ARM_AM::isSOImmTwoPartVal(Val)); // two instrs.
877877 if (UseCP) {
878878 SDValue CPIdx =
879 CurDAG->getTargetConstantPool(
880 CurDAG->getContext()->getConstantInt(Type::Int32Ty, Val),
879 CurDAG->getTargetConstantPool(ConstantInt::get(Type::Int32Ty, Val),
881880 TLI.getPointerTy());
882881
883882 SDNode *ResNode;
5757 unsigned PredReg) const {
5858 MachineFunction &MF = *MBB.getParent();
5959 MachineConstantPool *ConstantPool = MF.getConstantPool();
60 Constant *C =
61 MF.getFunction()->getContext().getConstantInt(Type::Int32Ty, Val);
60 Constant *C = ConstantInt::get(Type::Int32Ty, Val);
6261 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
6362
6463 BuildMI(MBB, MBBI, dl, TII.get(ARM::tLDRcp))
5151 unsigned PredReg) const {
5252 MachineFunction &MF = *MBB.getParent();
5353 MachineConstantPool *ConstantPool = MF.getConstantPool();
54 Constant *C =
55 MF.getFunction()->getContext().getConstantInt(Type::Int32Ty, Val);
54 Constant *C = ConstantInt::get(Type::Int32Ty, Val);
5655 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
5756
5857 BuildMI(MBB, MBBI, dl, TII.get(ARM::t2LDRpci))
304304 // val32 >= IMM_LOW + IMM_LOW * IMM_MULT) //always true
305305 break; //(zext (LDAH (LDA)))
306306 //Else use the constant pool
307 ConstantInt *C = CurDAG->getContext()->getConstantInt(Type::Int64Ty, uval);
307 ConstantInt *C = ConstantInt::get(Type::Int64Ty, uval);
308308 SDValue CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
309309 SDNode *Tmp = CurDAG->getTargetNode(Alpha::LDAHr, dl, MVT::i64, CPI,
310310 SDValue(getGlobalBaseReg(), 0));
34993499 if (!ITy->isPowerOf2ByteWidth())
35003500 // We have a bit width that doesn't match an even power-of-2 byte
35013501 // size. Consequently we must & the value with the type's bit mask
3502 BitMask = I.getContext().getConstantInt(ITy, ITy->getBitMask());
3502 BitMask = ConstantInt::get(ITy, ITy->getBitMask());
35033503 if (BitMask)
35043504 Out << "((";
35053505 writeOperand(Operand);
48884888
48894889 // Build some magic constants.
48904890 std::vector CV0;
4891 CV0.push_back(Context->getConstantInt(APInt(32, 0x45300000)));
4892 CV0.push_back(Context->getConstantInt(APInt(32, 0x43300000)));
4893 CV0.push_back(Context->getConstantInt(APInt(32, 0)));
4894 CV0.push_back(Context->getConstantInt(APInt(32, 0)));
4891 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
4892 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
4893 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
4894 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
48954895 Constant *C0 = Context->getConstantVector(CV0);
48964896 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
48974897
173173 else if (! Predicate_immU16(N)) {
174174 unsigned Val = cast(N)->getZExtValue();
175175 SDValue CPIdx =
176 CurDAG->getTargetConstantPool(
177 CurDAG->getContext()->getConstantInt(Type::Int32Ty, Val),
176 CurDAG->getTargetConstantPool(ConstantInt::get(Type::Int32Ty, Val),
178177 TLI.getPointerTy());
179178 return CurDAG->getTargetNode(XCore::LDWCP_lru6, dl, MVT::i32,
180179 MVT::Other, CPIdx,
637637 // Emit a GEP and load for each element of the struct.
638638 const Type *AgTy = cast(I->getType())->getElementType();
639639 const StructType *STy = cast(AgTy);
640 Value *Idxs[2] = { Context.getConstantInt(Type::Int32Ty, 0), 0 };
640 Value *Idxs[2] = { ConstantInt::get(Type::Int32Ty, 0), 0 };
641641 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
642 Idxs[1] = Context.getConstantInt(Type::Int32Ty, i);
642 Idxs[1] = ConstantInt::get(Type::Int32Ty, i);
643643 Value *Idx = GetElementPtrInst::Create(*AI, Idxs, Idxs+2,
644644 (*AI)->getName()+"."+utostr(i),
645645 Call);
664664 // Use i32 to index structs, and i64 for others (pointers/arrays).
665665 // This satisfies GEP constraints.
666666 const Type *IdxTy = (isa(ElTy) ? Type::Int32Ty : Type::Int64Ty);
667 Ops.push_back(Context.getConstantInt(IdxTy, *II));
667 Ops.push_back(ConstantInt::get(IdxTy, *II));
668668 // Keep track of the type we're currently indexing
669669 ElTy = cast(ElTy)->getTypeAtIndex(*II);
670670 }
757757 const Type *AgTy = cast(I->getType())->getElementType();
758758 Value *TheAlloca = new AllocaInst(AgTy, 0, "", InsertPt);
759759 const StructType *STy = cast(AgTy);
760 Value *Idxs[2] = { Context.getConstantInt(Type::Int32Ty, 0), 0 };
760 Value *Idxs[2] = { ConstantInt::get(Type::Int32Ty, 0), 0 };
761761
762762 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
763 Idxs[1] = Context.getConstantInt(Type::Int32Ty, i);
763 Idxs[1] = ConstantInt::get(Type::Int32Ty, i);
764764 Value *Idx =
765765 GetElementPtrInst::Create(TheAlloca, Idxs, Idxs+2,
766766 TheAlloca->getName()+"."+utostr(i),
487487 const StructLayout &Layout = *TD.getStructLayout(STy);
488488 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
489489 Constant *In = getAggregateConstantElement(Init,
490 Context.getConstantInt(Type::Int32Ty, i),
490 ConstantInt::get(Type::Int32Ty, i),
491491 Context);
492492 assert(In && "Couldn't get element of initializer?");
493493 GlobalVariable *NGV = new GlobalVariable(Context,
522522 unsigned EltAlign = TD.getABITypeAlignment(STy->getElementType());
523523 for (unsigned i = 0, e = NumElements; i != e; ++i) {
524524 Constant *In = getAggregateConstantElement(Init,
525 Context.getConstantInt(Type::Int32Ty, i),
525 ConstantInt::get(Type::Int32Ty, i),
526526 Context);
527527 assert(In && "Couldn't get element of initializer?");
528528
15071507 if (const ArrayType *AT = dyn_cast(MI->getAllocatedType())) {
15081508 MallocInst *NewMI =
15091509 new MallocInst(AllocSTy,
1510 Context.getConstantInt(Type::Int32Ty, AT->getNumElements()),
1510 ConstantInt::get(Type::Int32Ty, AT->getNumElements()),
15111511 "", MI);
15121512 NewMI->takeName(MI);
15131513 Value *Cast = new BitCastInst(NewMI, MI->getType(), "tmp", MI);
16041604 // Only do this if we weren't storing a loaded value.
16051605 Value *StoreVal;
16061606 if (StoringOther || SI->getOperand(0) == InitVal)
1607 StoreVal = Context.getConstantInt(Type::Int1Ty, StoringOther);
1607 StoreVal = ConstantInt::get(Type::Int1Ty, StoringOther);
16081608 else {
16091609 // Otherwise, we are storing a previously loaded copy. To do this,
16101610 // change the copy from copying the original value to just copying the
19461946 LLVMContext &Context) {
19471947 // If we made a change, reassemble the initializer list.
19481948 std::vector CSVals;
1949 CSVals.push_back(Context.getConstantInt(Type::Int32Ty, 65535));
1949 CSVals.push_back(ConstantInt::get(Type::Int32Ty, 65535));
19501950 CSVals.push_back(0);
19511951
19521952 // Create the new init list.
19581958 const Type *FTy = Context.getFunctionType(Type::VoidTy, false);
19591959 const PointerType *PFTy = Context.getPointerTypeUnqual(FTy);
19601960 CSVals[1] = Context.getNullValue(PFTy);
1961 CSVals[0] = Context.getConstantInt(Type::Int32Ty, 2147483647);
1961 CSVals[0] = ConstantInt::get(Type::Int32Ty, 2147483647);
19621962 }
19631963 CAList.push_back(Context.getConstantStruct(CSVals));
19641964 }
382382 new BitCastInst(Inst->getOperand(1), SBPTy, "SBJmpBuf", Inst);
383383 std::vector Args =
384384 make_vector(GetSetJmpMap(Func), BufPtr,
385 Inst->getContext().getConstantInt(Type::Int32Ty,
386 SetJmpIDMap[Func]++), 0);
385 ConstantInt::get(Type::Int32Ty,SetJmpIDMap[Func]++), 0);
387386 CallInst::Create(AddSJToMap, Args.begin(), Args.end(), "", Inst);
388387
389388 // We are guaranteed that there are no values live across basic blocks
433432
434433 // Add the case for this setjmp's number...
435434 SwitchValuePair SVP = GetSJSwitch(Func, GetRethrowBB(Func));
436 SVP.first->addCase(Inst->getContext().getConstantInt(Type::Int32Ty,
437 SetJmpIDMap[Func] - 1),
438 SetJmpContBlock);
435 SVP.first->addCase(ConstantInt::get(Type::Int32Ty, SetJmpIDMap[Func] - 1),
436 SetJmpContBlock);
439437
440438 // Value coming from the handling of the exception.
441439 PHI->addIncoming(SVP.second, SVP.second->getParent());
5454 // pass null.
5555 Args[2] = Context.getConstantPointerNull(UIntPtr);
5656 }
57 Args[3] = Context.getConstantInt(Type::Int32Ty, NumElements);
57 Args[3] = ConstantInt::get(Type::Int32Ty, NumElements);
5858
5959 Instruction *InitCall = CallInst::Create(InitFn, Args.begin(), Args.end(),
6060 "newargc", InsertPos);
110110 // Create the getelementptr constant expression
111111 std::vector Indices(2);
112112 Indices[0] = Context.getNullValue(Type::Int32Ty);
113 Indices[1] = Context.getConstantInt(Type::Int32Ty, CounterNum);
113 Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum);
114114 Constant *ElementPtr =
115115 Context.getConstantExprGetElementPtr(CounterArray, &Indices[0],
116116 Indices.size());
118118 // Load, increment and store the value back.
119119 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
120120 Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal,
121 Context.getConstantInt(Type::Int32Ty, 1),
121 ConstantInt::get(Type::Int32Ty, 1),
122122 "NewFuncCounter", InsertPos);
123123 new StoreInst(NewVal, ElementPtr, InsertPos);
124124 }
196196
197197 GlobalRandomCounter::GlobalRandomCounter(Module& M, const IntegerType* t,
198198 uint64_t resetval) : T(t) {
199 ConstantInt* Init = M.getContext().getConstantInt(T, resetval);
199 ConstantInt* Init = ConstantInt::get(T, resetval);
200200 ResetValue = Init;
201201 Counter = new GlobalVariable(M, T, false, GlobalValue::InternalLinkage,
202202 Init, "RandomSteeringCounter");
208208
209209 void GlobalRandomCounter::ProcessChoicePoint(BasicBlock* bb) {
210210 BranchInst* t = cast(bb->getTerminator());
211 LLVMContext &Context = bb->getContext();
212211
213212 //decrement counter
214213 LoadInst* l = new LoadInst(Counter, "counter", t);
215214
216215 ICmpInst* s = new ICmpInst(t, ICmpInst::ICMP_EQ, l,
217 Context.getConstantInt(T, 0),
216 ConstantInt::get(T, 0),
218217 "countercc");
219218
220 Value* nv = BinaryOperator::CreateSub(l, Context.getConstantInt(T, 1),
219 Value* nv = BinaryOperator::CreateSub(l, ConstantInt::get(T, 1),
221220 "counternew", t);
222221 new StoreInst(nv, Counter, t);
223222 t->setCondition(s);
235234 GlobalRandomCounterOpt::GlobalRandomCounterOpt(Module& M, const IntegerType* t,
236235 uint64_t resetval)
237236 : AI(0), T(t) {
238 ConstantInt* Init = M.getContext().getConstantInt(T, resetval);
237 ConstantInt* Init = ConstantInt::get(T, resetval);
239238 ResetValue = Init;
240239 Counter = new GlobalVariable(M, T, false, GlobalValue::InternalLinkage,
241240 Init, "RandomSteeringCounter");
282281
283282 void GlobalRandomCounterOpt::ProcessChoicePoint(BasicBlock* bb) {
284283 BranchInst* t = cast(bb->getTerminator());
285 LLVMContext &Context = bb->getContext();
286284
287285 //decrement counter
288286 LoadInst* l = new LoadInst(AI, "counter", t);
289287
290288 ICmpInst* s = new ICmpInst(t, ICmpInst::ICMP_EQ, l,
291 Context.getConstantInt(T, 0),
289 ConstantInt::get(T, 0),
292290 "countercc");
293291
294 Value* nv = BinaryOperator::CreateSub(l, Context.getConstantInt(T, 1),
292 Value* nv = BinaryOperator::CreateSub(l, ConstantInt::get(T, 1),
295293 "counternew", t);
296294 new StoreInst(nv, AI, t);
297295 t->setCondition(s);
317315
318316 void CycleCounter::ProcessChoicePoint(BasicBlock* bb) {
319317 BranchInst* t = cast(bb->getTerminator());
320 LLVMContext &Context = bb->getContext();
321318
322319 CallInst* c = CallInst::Create(F, "rdcc", t);
323320 BinaryOperator* b =
324 BinaryOperator::CreateAnd(c, Context.getConstantInt(Type::Int64Ty, rm),
321 BinaryOperator::CreateAnd(c, ConstantInt::get(Type::Int64Ty, rm),
325322 "mrdcc", t);
326323
327324 ICmpInst *s = new ICmpInst(t, ICmpInst::ICMP_EQ, b,
328 Context.getConstantInt(Type::Int64Ty, 0),
325 ConstantInt::get(Type::Int64Ty, 0),
329326 "mrdccc");
330327
331328 t->setCondition(s);
352349 // Create the getelementptr constant expression
353350 std::vector Indices(2);
354351 Indices[0] = BB->getContext().getNullValue(Type::Int32Ty);
355 Indices[1] = BB->getContext().getConstantInt(Type::Int32Ty, CounterNum);
352 Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum);
356353 Constant *ElementPtr =
357354 BB->getContext().getConstantExprGetElementPtr(CounterArray,
358355 &Indices[0], 2);
361358 Value *OldVal = new LoadInst(ElementPtr, "OldCounter", InsertPos);
362359 profcode.insert(OldVal);
363360 Value *NewVal = BinaryOperator::CreateAdd(OldVal,
364 BB->getContext().getConstantInt(Type::Int32Ty, 1),
361 ConstantInt::get(Type::Int32Ty, 1),
365362 "NewCounter", InsertPos);
366363 profcode.insert(NewVal);
367364 profcode.insert(new StoreInst(NewVal, ElementPtr, InsertPos));
482479 //b:
483480 BranchInst::Create(cast(Translate(dst)), bbC);
484481 BranchInst::Create(dst, cast(Translate(dst)),
485 F.getContext().getConstantInt(Type::Int1Ty, true), bbCp);
482 ConstantInt::get(Type::Int1Ty, true), bbCp);
486483 //c:
487484 {
488485 TerminatorInst* iB = src->getTerminator();
539536 ReplaceInstWithInst(T, BranchInst::Create(T->getSuccessor(0),
540537 cast(
541538 Translate(T->getSuccessor(0))),
542 F.getContext().getConstantInt(Type::Int1Ty,
543 true)));
539 ConstantInt::get(Type::Int1Ty, true)));
544540
545541 //do whatever is needed now that the function is duplicated
546542 c->PrepFunction(&F);
616616 V = new SExtInst(V, IntPtrTy, "sunkaddr", InsertPt);
617617 }
618618 if (AddrMode.Scale != 1)
619 V = BinaryOperator::CreateMul(V, Context.getConstantInt(IntPtrTy,
619 V = BinaryOperator::CreateMul(V, ConstantInt::get(IntPtrTy,
620620 AddrMode.Scale),
621621 "sunkaddr", InsertPt);
622622 Result = V;
648648
649649 // Add in the Base Offset if present.
650650 if (AddrMode.BaseOffs) {
651 Value *V = Context.getConstantInt(IntPtrTy, AddrMode.BaseOffs);
651 Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
652652 if (Result)
653653 Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
654654 else
717717 // Insert new integer induction variable.
718718 PHINode *NewPHI = PHINode::Create(Type::Int32Ty,
719719 PH->getName()+".int", PH);
720 NewPHI->addIncoming(Context.getConstantInt(Type::Int32Ty, newInitValue),
720 NewPHI->addIncoming(ConstantInt::get(Type::Int32Ty, newInitValue),
721721 PH->getIncomingBlock(IncomingEdge));
722722
723723 Value *NewAdd = BinaryOperator::CreateAdd(NewPHI,
724 Context.getConstantInt(Type::Int32Ty,
724 ConstantInt::get(Type::Int32Ty,
725725 newIncrValue),
726726 Incr->getName()+".int", Incr);
727727 NewPHI->addIncoming(NewAdd, PH->getIncomingBlock(BackEdge));
728728
729729 // The back edge is edge 1 of newPHI, whatever it may have been in the
730730 // original PHI.
731 ConstantInt *NewEV = Context.getConstantInt(Type::Int32Ty, intEV);
731 ConstantInt *NewEV = ConstantInt::get(Type::Int32Ty, intEV);
732732 Value *LHS = (EVIndex == 1 ? NewPHI->getIncomingValue(1) : NewEV);
733733 Value *RHS = (EVIndex == 1 ? NewEV : NewPHI->getIncomingValue(1));
734734 ICmpInst *NewEC = new ICmpInst(EC->getParent()->getTerminator(),
598598
599599 // Constants can be considered to be not'ed values...
600600 if (ConstantInt *C = dyn_cast(V))
601 return Context->getConstantInt(~C->getValue());
601 return ConstantInt::get(*Context, ~C->getValue());
602602 return 0;
603603 }
604604
619619 // The multiplier is really 1 << CST.
620620 uint32_t BitWidth = cast(V->getType())->getBitWidth();
621621 uint32_t CSTVal = CST->getLimitedValue(BitWidth);
622 CST = Context->getConstantInt(APInt(BitWidth, 1).shl(CSTVal));
622 CST = ConstantInt::get(*Context, APInt(BitWidth, 1).shl(CSTVal));
623623 return I->getOperand(0);
624624 }
625625 }
639639 /// AddOne - Add one to a ConstantInt
640640 static Constant *AddOne(Constant *C, LLVMContext *Context) {
641641 return Context->getConstantExprAdd(C,
642 Context->getConstantInt(C->getType(), 1));
642 ConstantInt::get(C->getType(), 1));
643643 }
644644 /// SubOne - Subtract one from a ConstantInt
645645 static Constant *SubOne(ConstantInt *C, LLVMContext *Context) {
646646 return Context->getConstantExprSub(C,
647 Context->getConstantInt(C->getType(), 1));
647 ConstantInt::get(C->getType(), 1));
648648 }
649649 /// MultiplyOverflows - True if the multiply can not be expressed in an int
650650 /// this size.
691691
692692 // This instruction is producing bits that are not demanded. Shrink the RHS.
693693 Demanded &= OpC->getValue();
694 I->setOperand(OpNo, Context->getConstantInt(Demanded));
694 I->setOperand(OpNo, ConstantInt::get(*Context, Demanded));
695695 return true;
696696 }
697697
10221022 if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
10231023 // all known
10241024 if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
1025 Constant *AndC = Context->getConstantInt(~RHSKnownOne & DemandedMask);
1025 Constant *AndC = ConstantInt::get(*Context,
1026 ~RHSKnownOne & DemandedMask);
10261027 Instruction *And =
10271028 BinaryOperator::CreateAnd(I->getOperand(0), AndC, "tmp");
10281029 return InsertNewInstBefore(And, *I);
13941395 Instruction *NewVal;
13951396 if (InputBit > ResultBit)
13961397 NewVal = BinaryOperator::CreateLShr(I->getOperand(1),
1397 Context->getConstantInt(I->getType(), InputBit-ResultBit));
1398 ConstantInt::get(I->getType(), InputBit-ResultBit));
13981399 else
13991400 NewVal = BinaryOperator::CreateShl(I->getOperand(1),
1400 Context->getConstantInt(I->getType(), ResultBit-InputBit));
1401 ConstantInt::get(I->getType(), ResultBit-InputBit));
14011402 NewVal->takeName(I);
14021403 return InsertNewInstBefore(NewVal, *I);
14031404 }
14141415 // If the client is only demanding bits that we know, return the known
14151416 // constant.
14161417 if ((DemandedMask & (RHSKnownZero|RHSKnownOne)) == DemandedMask) {
1417 Constant *C = Context->getConstantInt(RHSKnownOne);
1418 Constant *C = ConstantInt::get(*Context, RHSKnownOne);
14181419 if (isa(V->getType()))
14191420 C = Context->getConstantExprIntToPtr(C, V->getType());
14201421 return C;
16011602 if (UndefElts[i])
16021603 Elts.push_back(Context->getUndef(Type::Int32Ty));
16031604 else
1604 Elts.push_back(Context->getConstantInt(Type::Int32Ty,
1605 Elts.push_back(ConstantInt::get(Type::Int32Ty,
16051606 Shuffle->getMaskValue(i)));
16061607 }
16071608 I->setOperand(2, Context->getConstantVector(Elts));
17321733 Value *RHS = II->getOperand(2);
17331734 // Extract the element as scalars.
17341735 LHS = InsertNewInstBefore(new ExtractElementInst(LHS,
1735 Context->getConstantInt(Type::Int32Ty, 0U, false), "tmp"), *II);
1736 ConstantInt::get(Type::Int32Ty, 0U, false), "tmp"), *II);
17361737 RHS = InsertNewInstBefore(new ExtractElementInst(RHS,
1737 Context->getConstantInt(Type::Int32Ty, 0U, false), "tmp"), *II);
1738 ConstantInt::get(Type::Int32Ty, 0U, false), "tmp"), *II);
17381739
17391740 switch (II->getIntrinsicID()) {
17401741 default: llvm_unreachable("Case stmts out of sync!");
17531754 Instruction *New =
17541755 InsertElementInst::Create(
17551756 Context->getUndef(II->getType()), TmpV,
1756 Context->getConstantInt(Type::Int32Ty, 0U, false), II->getName());
1757 ConstantInt::get(Type::Int32Ty, 0U, false), II->getName());
17571758 InsertNewInstBefore(New, *II);
17581759 AddSoonDeadInstToWorklist(*II, 0);
17591760 return New;
18591860 bool shouldApply(Value *LHS) const { return LHS == RHS; }
18601861 Instruction *apply(BinaryOperator &Add) const {
18611862 return BinaryOperator::CreateShl(Add.getOperand(0),
1862 Context->getConstantInt(Add.getType(), 1));
1863 ConstantInt::get(Add.getType(), 1));
18631864 }
18641865 };
18651866
25422543 ConstantInt *C2 = 0;
25432544 if (dyn_castFoldableMul(Op1I, C2, Context) == Op0) {
25442545 Constant *CP1 =
2545 Context->getConstantExprSub(Context->getConstantInt(I.getType(), 1),
2546 Context->getConstantExprSub(ConstantInt::get(I.getType(), 1),
25462547 C2);
25472548 return BinaryOperator::CreateMul(Op0, CP1);
25482549 }
26532654 const APInt& Val = cast(CI)->getValue();
26542655 if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C
26552656 return BinaryOperator::CreateShl(Op0,
2656 Context->getConstantInt(Op0->getType(), Val.logBase2()));
2657 ConstantInt::get(Op0->getType(), Val.logBase2()));
26572658 }
26582659 } else if (isa(Op1->getType())) {
26592660 if (Op1->isNullValue())
27602761 isSignBitCheck(SCI->getPredicate(), cast(SCIOp1), TIS) &&
27612762 TIS) {
27622763 // Shift the X value right to turn it into "all signbits".
2763 Constant *Amt = Context->getConstantInt(SCIOp0->getType(),
2764 Constant *Amt = ConstantInt::get(SCIOp0->getType(),
27642765 SCOpTy->getPrimitiveSizeInBits()-1);
27652766 Value *V =
27662767 InsertNewInstBefore(
29312932 // (sdiv X, X) --> 1 (udiv X, X) --> 1
29322933 if (Op0 == Op1) {
29332934 if (const VectorType *Ty = dyn_cast(I.getType())) {
2934 Constant *CI = Context->getConstantInt(Ty->getElementType(), 1);
2935 Constant *CI = ConstantInt::get(Ty->getElementType(), 1);
29352936 std::vector Elts(Ty->getNumElements(), CI);
29362937 return ReplaceInstUsesWith(I, Context->getConstantVector(Elts));
29372938 }
29382939
2939 Constant *CI = Context->getConstantInt(I.getType(), 1);
2940 Constant *CI = ConstantInt::get(I.getType(), 1);
29402941 return ReplaceInstUsesWith(I, CI);
29412942 }
29422943
30073008 // if so, convert to a right shift.
30083009 if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2
30093010 return BinaryOperator::CreateLShr(Op0,
3010 Context->getConstantInt(Op0->getType(), C->getValue().logBase2()));
3011 ConstantInt::get(Op0->getType(), C->getValue().logBase2()));
30113012
30123013 // X udiv C, where C >= signbit
30133014 if (C->getValue().isNegative()) {
30153016 ICmpInst::ICMP_ULT, Op0, C),
30163017 I);
30173018 return SelectInst::Create(IC, Context->getNullValue(I.getType()),
3018 Context->getConstantInt(I.getType(), 1));
3019 ConstantInt::get(I.getType(), 1));
30193020 }
30203021 }
30213022
30283029 Value *N = RHSI->getOperand(1);
30293030 const Type *NTy = N->getType();
30303031 if (uint32_t C2 = C1.logBase2()) {
3031 Constant *C2V = Context->getConstantInt(NTy, C2);
3032 Constant *C2V = ConstantInt::get(NTy, C2);
30323033 N = InsertNewInstBefore(BinaryOperator::CreateAdd(N, C2V, "tmp"), I);
30333034 }
30343035 return BinaryOperator::CreateLShr(Op0, N);
30463047 // Compute the shift amounts
30473048 uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2();
30483049 // Construct the "on true" case of the select
3049 Constant *TC = Context->getConstantInt(Op0->getType(), TSA);
3050 Constant *TC = ConstantInt::get(Op0->getType(), TSA);
30503051 Instruction *TSI = BinaryOperator::CreateLShr(
30513052 Op0, TC, SI->getName()+".t");
30523053 TSI = InsertNewInstBefore(TSI, I);
30533054
30543055 // Construct the "on false" case of the select
3055 Constant *FC = Context->getConstantInt(Op0->getType(), FSA);
3056 Constant *FC = ConstantInt::get(Op0->getType(), FSA);
30563057 Instruction *FSI = BinaryOperator::CreateLShr(
30573058 Op0, FC, SI->getName()+".f");
30583059 FSI = InsertNewInstBefore(FSI, I);
35763577 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
35773578 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
35783579 APInt ShlMask(APInt::getHighBitsSet(BitWidth, BitWidth-OpRHSVal));
3579 ConstantInt *CI = Context->getConstantInt(AndRHS->getValue() & ShlMask);
3580 ConstantInt *CI = ConstantInt::get(*Context, AndRHS->getValue() & ShlMask);
35803581
35813582 if (CI->getValue() == ShlMask) {
35823583 // Masking out bits that the shift already masks
35963597 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
35973598 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
35983599 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
3599 ConstantInt *CI = Context->getConstantInt(AndRHS->getValue() & ShrMask);
3600 ConstantInt *CI = ConstantInt::get(*Context, AndRHS->getValue() & ShrMask);
36003601
36013602 if (CI->getValue() == ShrMask) {
36023603 // Masking out bits that the shift already masks.
36153616 uint32_t BitWidth = AndRHS->getType()->getBitWidth();
36163617 uint32_t OpRHSVal = OpRHS->getLimitedValue(BitWidth);
36173618 APInt ShrMask(APInt::getLowBitsSet(BitWidth, BitWidth - OpRHSVal));
3618 Constant *C = Context->getConstantInt(AndRHS->getValue() & ShrMask);
3619 Constant *C = ConstantInt::get(*Context, AndRHS->getValue() & ShrMask);
36193620 if (C == AndRHS) { // Masking out bits shifted in.
36203621 // (Val ashr C1) & C2 -> (Val lshr C1) & C2
36213622 // Make the argument unsigned.
38553856 Val->getName()+".off");
38563857 InsertNewInstBefore(Add, I);
38573858 return new ICmpInst(*Context, ICmpInst::ICMP_UGT, Add,
3858 Context->getConstantInt(Add->getType(), 1));
3859 ConstantInt::get(Add->getType(), 1));
38593860 }
38603861 break; // (X != 13 & X != 15) -> no change
38613862 }
47694770 InsertNewInstBefore(Or, I);
47704771 Or->takeName(Op0);
47714772 return BinaryOperator::CreateAnd(Or,
4772 Context->getConstantInt(RHS->getValue() | C1->getValue()));
4773 ConstantInt::get(*Context, RHS->getValue() | C1->getValue()));
47734774 }
47744775
47754776 // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
47794780 InsertNewInstBefore(Or, I);
47804781 Or->takeName(Op0);
47814782 return BinaryOperator::CreateXor(Or,
4782 Context->getConstantInt(C1->getValue() & ~RHS->getValue()));
4783 ConstantInt::get(*Context, C1->getValue() & ~RHS->getValue()));
47834784 }
47844785
47854786 // Try to fold constant and into select arguments.
51065107 if (Constant *Op0I0C = dyn_cast(Op0I->getOperand(0))) {
51075108 Constant *NegOp0I0C = Context->getConstantExprNeg(Op0I0C);
51085109 Constant *ConstantRHS = Context->getConstantExprSub(NegOp0I0C,
5109 Context->getConstantInt(I.getType(), 1));
5110 ConstantInt::get(I.getType(), 1));
51105111 return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
51115112 }
51125113
51175118 Constant *NegOp0CI = Context->getConstantExprNeg(Op0CI);
51185119 return BinaryOperator::CreateSub(
51195120 Context->getConstantExprSub(NegOp0CI,
5120 Context->getConstantInt(I.getType(), 1)),
5121 ConstantInt::get(I.getType(), 1)),
51215122 Op0I->getOperand(0));
51225123 } else if (RHS->getValue().isSignBit()) {
51235124 // (X + C) ^ signbit -> (X + C + signbit)
5124 Constant *C =
5125 Context->getConstantInt(RHS->getValue() + Op0CI->getValue());
5125 Constant *C = ConstantInt::get(*Context,
5126 RHS->getValue() + Op0CI->getValue());
51265127 return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
51275128
51285129 }
53265327
53275328 if (const VectorType *VTy = dyn_cast(In1->getType())) {
53285329 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
5329 Constant *Idx = Context->getConstantInt(Type::Int32Ty, i);
5330 Constant *Idx = ConstantInt::get(Type::Int32Ty, i);
53305331 if (HasAddOverflow(ExtractElement(Result, Idx, Context),
53315332 ExtractElement(In1, Idx, Context),
53325333 ExtractElement(In2, Idx, Context),
53625363
53635364 if (const VectorType *VTy = dyn_cast(In1->getType())) {
53645365 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
5365 Constant *Idx = Context->getConstantInt(Type::Int32Ty, i);
5366 Constant *Idx = ConstantInt::get(Type::Int32Ty, i);
53665367 if (HasSubOverflow(ExtractElement(Result, Idx, Context),
53675368 ExtractElement(In1, Idx, Context),
53685369 ExtractElement(In2, Idx, Context),
54045405
54055406 if (ConstantInt *RC = dyn_cast(Result))
54065407 Result =
5407 Context->getConstantInt(RC->getValue() + APInt(IntPtrWidth, Size));
5408 ConstantInt::get(*Context,
5409 RC->getValue() + APInt(IntPtrWidth, Size));
54085410 else
54095411 Result = IC.InsertNewInstBefore(
54105412 BinaryOperator::CreateAdd(Result,
5411 Context->getConstantInt(IntPtrTy, Size),
5413 ConstantInt::get(IntPtrTy, Size),
54125414 GEP->getName()+".offs"), I);
54135415 continue;
54145416 }
54155417
5416 Constant *Scale = Context->getConstantInt(IntPtrTy, Size);
5418 Constant *Scale = ConstantInt::get(IntPtrTy, Size);
54175419 Constant *OC =
54185420 Context->getConstantExprIntegerCast(OpC, IntPtrTy, true /*SExt*/);
54195421 Scale = Context->getConstantExprMul(OC, Scale);
54375439 Op->getName()+".c"), I);
54385440 }
54395441 if (Size != 1) {
5440 Constant *Scale = Context->getConstantInt(IntPtrTy, Size);
5442 Constant *Scale = ConstantInt::get(IntPtrTy, Size);
54415443 if (Constant *OpC = dyn_cast(Op))
54425444 Op = Context->getConstantExprMul(OpC, Scale);
54435445 else // We'll let instcombine(mul) convert this to a shl if possible.
55585560 VariableIdx = CastInst::CreateIntegerCast(VariableIdx, IntPtrTy,
55595561 true /*SExt*/,
55605562 VariableIdx->getNameStart(), &I);
5561 Constant *OffsetVal = IC.getContext()->getConstantInt(IntPtrTy, NewOffs);
5563 Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs);
55625564 return BinaryOperator::CreateAdd(VariableIdx, OffsetVal, "offset", &I);
55635565 }
55645566
56525654
56535655 if (NumDifferences == 0) // SAME GEP?
56545656 return ReplaceInstUsesWith(I, // No comparison is needed here.
5655 Context->getConstantInt(Type::Int1Ty,
5657 ConstantInt::get(Type::Int1Ty,
56565658 ICmpInst::isTrueWhenEqual(Cond)));
56575659
56585660 else if (NumDifferences == 1) {
59845986
59855987 // icmp X, X
59865988 if (Op0 == Op1)
5987 return ReplaceInstUsesWith(I, Context->getConstantInt(Type::Int1Ty,
5989 return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
59885990 I.isTrueWhenEqual()));
59895991
59905992 if (isa(Op1)) // X icmp undef -> undef
59965998 isa(Op0)) &&
59975999 (isa(Op1) || isa(Op1) ||
59986000 isa(Op1)))
5999 return ReplaceInstUsesWith(I, Context->getConstantInt(Type::Int1Ty,
6001 return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
60006002 !I.isTrueWhenEqual()));
60016003
60026004 // icmp's with boolean values can always be turned into bitwise operations
61406142 // that code below can assume that Min != Max.
61416143 if (!isa(Op0) && Op0Min == Op0Max)
61426144 return new ICmpInst(*Context, I.getPredicate(),
6143 Context->getConstantInt(Op0Min), Op1);
6145 ConstantInt::get(*Context, Op0Min), Op1);
61446146 if (!isa(Op1) && Op1Min == Op1Max)
61456147 return new ICmpInst(*Context, I.getPredicate(), Op0,
6146 Context->getConstantInt(Op1Min));
6148 ConstantInt::get(*Context, Op1Min));
61476149
61486150 // Based on the range information we know about the LHS, see if we can
61496151 // simplify this comparison. For example, (x&4) < 8 is always true.
63436345 // can assume it is successful and remove the malloc.
63446346 if (LHSI->hasOneUse() && isa(RHSC)) {
63456347 AddToWorkList(LHSI);
6346 return ReplaceInstUsesWith(I, Context->getConstantInt(Type::Int1Ty,
6348 return ReplaceInstUsesWith(I, ConstantInt::get(Type::Int1Ty,
63476349 !I.isTrueWhenEqual()));
63486350 }
63496351 break;
64416443 // Mask = -1 >> count-trailing-zeros(Cst).
64426444 if (!CI->isZero() && !CI->isOne()) {
64436445 const APInt &AP = CI->getValue();
6444 ConstantInt *Mask = Context->getConstantInt(
6446 ConstantInt *Mask = ConstantInt::get(*Context,
64456447 APInt::getLowBitsSet(AP.getBitWidth(),
64466448 AP.getBitWidth() -
64476449 AP.countTrailingZeros()));
64886490 if (match(B, m_ConstantInt(C1), *Context) &&
64896491 match(D, m_ConstantInt(C2), *Context) && Op1->hasOneUse()) {
64906492 Constant *NC =
6491 Context->getConstantInt(C1->getValue() ^ C2->getValue());
6493 ConstantInt::get(*Context, C1->getValue() ^ C2->getValue());
64926494 Instruction *Xor = BinaryOperator::CreateXor(C, NC, "tmp");
64936495 return new ICmpInst(*Context, I.getPredicate(), A,
64946496 InsertNewInstBefore(Xor, I));
67266728 NewRHS.zext(SrcBits);
67276729 NewRHS |= KnownOne;
67286730 return new ICmpInst(*Context, ICI.getPredicate(), LHSI->getOperand(0),
6729 Context->getConstantInt(NewRHS));
6731 ConstantInt::get(*Context, NewRHS));
67306732 }
67316733 }
67326734 break;
67696771 ? ICI.getUnsignedPredicate()
67706772 : ICI.getSignedPredicate();
67716773 return new ICmpInst(*Context, Pred, LHSI->getOperand(0),
6772 Context->getConstantInt(RHSV ^ SignBit));
6774 ConstantInt::get(*Context, RHSV ^ SignBit));
67736775 }
67746776
67756777 // (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A)
67806782 : ICI.getSignedPredicate();
67816783 Pred = ICI.getSwappedPredicate(Pred);
67826784 return new ICmpInst(*Context, Pred, LHSI->getOperand(0),
6783 Context->getConstantInt(RHSV ^ NotSignBit));
6785 ConstantInt::get(*Context, RHSV ^ NotSignBit));
67846786 }
67856787 }
67866788 }
68096811 NewCI.zext(BitWidth);
68106812 Instruction *NewAnd =
68116813 BinaryOperator::CreateAnd(Cast->getOperand(0),
6812 Context->getConstantInt(NewCST),LHSI->getName());
6814 ConstantInt::get(*Context, NewCST), LHSI->getName());
68136815 InsertNewInstBefore(NewAnd, ICI);
68146816 return new ICmpInst(*Context, ICI.getPredicate(), NewAnd,
6815 Context->getConstantInt(NewCI));
6817 ConstantInt::get(*Context, NewCI));
68166818 }
68176819 }
68186820
69296931 ShAmt);
69306932 if (Comp != RHS) {// Comparing against a bit that we know is zero.
69316933 bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
6932 Constant *Cst = Context->getConstantInt(Type::Int1Ty, IsICMP_NE);
6934 Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE);
69336935 return ReplaceInstUsesWith(ICI, Cst);
69346936 }
69356937
69376939 // Otherwise strength reduce the shift into an and.
69386940 uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
69396941 Constant *Mask =
6940 Context->getConstantInt(APInt::getLowBitsSet(TypeBits,
6942 ConstantInt::get(*Context, APInt::getLowBitsSet(TypeBits,
69416943 TypeBits-ShAmtVal));
69426944
69436945 Instruction *AndI =
69456947 Mask, LHSI->getName()+".mask");
69466948 Value *And = InsertNewInstBefore(AndI, ICI);
69476949 return new ICmpInst(*Context, ICI.getPredicate(), And,
6948 Context->getConstantInt(RHSV.lshr(ShAmtVal)));
6950 ConstantInt::get(*Context, RHSV.lshr(ShAmtVal)));
69496951 }
69506952 }
69516953
69546956 if (LHSI->hasOneUse() &&
69556957 isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) {
69566958 // (X << 31) (X&1) != 0
6957 Constant *Mask = Context->getConstantInt(APInt(TypeBits, 1) <<
6959 Constant *Mask = ConstantInt::get(*Context, APInt(TypeBits, 1) <<
69586960 (TypeBits-ShAmt->getZExtValue()-1));
69596961 Instruction *AndI =
69606962 BinaryOperator::CreateAnd(LHSI->getOperand(0),
69936995
69946996 if (Comp != RHSV) { // Comparing against a bit that we know is zero.
69956997 bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
6996 Constant *Cst = Context->getConstantInt(Type::Int1Ty, IsICMP_NE);
6998 Constant *Cst = ConstantInt::get(Type::Int1Ty, IsICMP_NE);
69976999 return ReplaceInstUsesWith(ICI, Cst);
69987000 }
69997001
70107012 if (LHSI->hasOneUse()) {
70117013 // Otherwise strength reduce the shift into an and.
70127014 APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
7013 Constant *Mask = Context->getConstantInt(Val);
7015 Constant *Mask = ConstantInt::get(*Context, Val);
70147016
70157017 Instruction *AndI =
70167018 BinaryOperator::CreateAnd(LHSI->getOperand(0),
70507052 if (ICI.isSignedPredicate()) {
70517053 if (CR.getLower().isSignBit()) {
70527054 return new ICmpInst(*Context, ICmpInst::ICMP_SLT, LHSI->getOperand(0),
7053 Context->getConstantInt(CR.getUpper()));
7055 ConstantInt::get(*Context, CR.getUpper()));
70547056 } else if (CR.getUpper().isSignBit()) {
70557057 return new ICmpInst(*Context, ICmpInst::ICMP_SGE, LHSI->getOperand(0),
7056 Context->getConstantInt(CR.getLower()));
7058 ConstantInt::get(*Context, CR.getLower()));
70577059 }
70587060 } else {
70597061 if (CR.getLower().isMinValue()) {
70607062 return new ICmpInst(*Context, ICmpInst::ICMP_ULT, LHSI->getOperand(0),
7061 Context->getConstantInt(CR.getUpper()));
7063 ConstantInt::get(*Context, CR.getUpper()));
70627064 } else if (CR.getUpper().isMinValue()) {
70637065 return new ICmpInst(*Context, ICmpInst::ICMP_UGE, LHSI->getOperand(0),
7064 Context->getConstantInt(CR.getLower()));
7066 ConstantInt::get(*Context, CR.getLower()));
70657067 }
70667068 }
70677069 }
71357137 Constant *NotCI = Context->getConstantExprNot(RHS);
71367138 if (!Context->getConstantExprAnd(BOC, NotCI)->isNullValue())
71377139 return ReplaceInstUsesWith(ICI,
7138 Context->getConstantInt(Type::Int1Ty,
7140 ConstantInt::get(Type::Int1Ty,
71397141 isICMP_NE));
71407142 }
71417143 break;
71467148 // comparison can never succeed!
71477149 if ((RHSV & ~BOC->getValue()) != 0)
71487150 return ReplaceInstUsesWith(ICI,
7149 Context->getConstantInt(Type::Int1Ty,
7151 ConstantInt::get(Type::Int1Ty,
71507152 isICMP_NE));
71517153
71527154 // If we have ((X & C) == C), turn it into ((X & C) != 0).
71807182 if (II->getIntrinsicID() == Intrinsic::bswap) {
71817183 AddToWorkList(II);
71827184 ICI.setOperand(0, II->getOperand(1));
7183 ICI.setOperand(1, Context->getConstantInt(RHSV.byteSwap()));
7185 ICI.setOperand(1, ConstantInt::get(*Context, RHSV.byteSwap()));
71847186 return &ICI;
71857187 }
71867188 }
74107412 if (I.getOpcode() != Instruction::AShr)
74117413 return ReplaceInstUsesWith(I, Context->getNullValue(Op0->getType()));
74127414 else {
7413 I.setOperand(1, Context->getConstantInt(I.getType(), TypeBits-1));
7415 I.setOperand(1, ConstantInt::get(I.getType(), TypeBits-1));
74147416 return &I;
74157417 }
74167418 }
74667468 }
74677469
74687470 Instruction *And =
7469 BinaryOperator::CreateAnd(NSh, Context->getConstantInt(MaskV),
7471 BinaryOperator::CreateAnd(NSh, ConstantInt::get(*Context, MaskV),
74707472 TI->getName());
74717473 InsertNewInstBefore(And, I); // shift1 & 0x00FF
74727474
75007502 Op0BO->getOperand(1)->getName());
75017503 InsertNewInstBefore(X, I); // (X + (Y << C))
75027504 uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
7503 return BinaryOperator::CreateAnd(X, Context->getConstantInt(
7505 return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context,
75047506 APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
75057507 }
75067508
75407542 Op0BO->getOperand(0)->getName());
75417543 InsertNewInstBefore(X, I); // (X + (Y << C))
75427544 uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
7543 return BinaryOperator::CreateAnd(X, Context->getConstantInt(
7545 return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context,
75447546 APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
75457547 }
75467548
76417643 }
76427644
76437645 return BinaryOperator::Create(I.getOpcode(), X,
7644 Context->getConstantInt(Ty, AmtSum));
7646 ConstantInt::get(Ty, AmtSum));
76457647 } else if (ShiftOp->getOpcode() == Instruction::LShr &&
76467648 I.getOpcode() == Instruction::AShr) {
76477649 if (AmtSum >= TypeBits)
76487650 return ReplaceInstUsesWith(I, Context->getNullValue(I.getType()));
76497651
76507652 // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0.
7651 return BinaryOperator::CreateLShr(X, Context->getConstantInt(Ty, AmtSum));
7653 return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum));
76527654 } else if (ShiftOp->getOpcode() == Instruction::AShr &&
76537655 I.getOpcode() == Instruction::LShr) {
76547656 // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0.
76567658 AmtSum = TypeBits-1;
76577659
76587660 Instruction *Shift =
7659 BinaryOperator::CreateAShr(X, Context->getConstantInt(Ty, AmtSum));
7661 BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum));
76607662 InsertNewInstBefore(Shift, I);
76617663
76627664 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
7663 return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask));
7665 return BinaryOperator::CreateAnd(Shift, ConstantInt::get(*Context, Mask));
76647666 }
76657667
76667668 // Okay, if we get here, one shift must be left, and the other shift must be
76697671 // If we have ((X >>? C) << C), turn this into X & (-1 << C).
76707672 if (I.getOpcode() == Instruction::Shl) {
76717673 APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1));
7672 return BinaryOperator::CreateAnd(X, Context->getConstantInt(Mask));
7674 return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context, Mask));
76737675 }
76747676 // If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
76757677 if (I.getOpcode() == Instruction::LShr) {
76767678 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
7677 return BinaryOperator::CreateAnd(X, Context->getConstantInt(Mask));
7679 return BinaryOperator::CreateAnd(X, ConstantInt::get(*Context, Mask));
76787680 }
76797681 // We can simplify ((X << C) >>s C) into a trunc + sext.
76807682 // NOTE: we could do this for any C, but that would make 'unusual' integer
77067708 assert(ShiftOp->getOpcode() == Instruction::LShr ||
77077709 ShiftOp->getOpcode() == Instruction::AShr);
77087710 Instruction *Shift =
7709 BinaryOperator::CreateShl(X, Context->getConstantInt(Ty, ShiftDiff));
7711 BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff));
77107712 InsertNewInstBefore(Shift, I);
77117713
77127714 APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
7713 return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask));
7715 return BinaryOperator::CreateAnd(Shift,
7716 ConstantInt::get(*Context, Mask));
77147717 }
77157718
77167719 // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2)
77177720 if (I.getOpcode() == Instruction::LShr) {
77187721 assert(ShiftOp->getOpcode() == Instruction::Shl);
77197722 Instruction *Shift =
7720 BinaryOperator::CreateLShr(X, Context->getConstantInt(Ty, ShiftDiff));
7723 BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, ShiftDiff));
77217724 InsertNewInstBefore(Shift, I);
77227725
77237726 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
7724 return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask));
7727 return BinaryOperator::CreateAnd(Shift,
7728 ConstantInt::get(*Context, Mask));
77257729 }
77267730
77277731 // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in.
77357739 ShiftOp->getOpcode() == Instruction::AShr);
77367740 Instruction *Shift =
77377741 BinaryOperator::Create(ShiftOp->getOpcode(), X,
7738 Context->getConstantInt(Ty, ShiftDiff));
7742 ConstantInt::get(Ty, ShiftDiff));
77397743 InsertNewInstBefore(Shift, I);
77407744
77417745 APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
7742 return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask));
7746 return BinaryOperator::CreateAnd(Shift,
7747 ConstantInt::get(*Context, Mask));
77437748 }
77447749
77457750 // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2)
77467751 if (I.getOpcode() == Instruction::LShr) {
77477752 assert(ShiftOp->getOpcode() == Instruction::Shl);
77487753 Instruction *Shift =
7749 BinaryOperator::CreateShl(X, Context->getConstantInt(Ty, ShiftDiff));
7754 BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff));
77507755 InsertNewInstBefore(Shift, I);
77517756
77527757 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
7753 return BinaryOperator::CreateAnd(Shift, Context->getConstantInt(Mask));
7758 return BinaryOperator::CreateAnd(Shift,
7759 ConstantInt::get(*Context, Mask));
77547760 }
77557761
77567762 // We can't handle (X << C1) >>a C2, it shifts arbitrary bits in.
77707776 if (ConstantInt *CI = dyn_cast(Val)) {
77717777 Offset = CI->getZExtValue();
77727778 Scale = 0;
7773 return Context->getConstantInt(Type::Int32Ty, 0);
7779 return ConstantInt::get(Type::Int32Ty, 0);
77747780 } else if (BinaryOperator *I = dyn_cast(Val)) {
77757781 if (ConstantInt *RHS = dyn_cast(I->getOperand(1))) {
77767782 if (I->getOpcode() == Instruction::Shl) {
78677873 Amt = NumElements;
78687874 } else {
78697875 // If the allocation size is constant, form a constant mul expression
7870 Amt = Context->getConstantInt(Type::Int32Ty, Scale);
7876 Amt = ConstantInt::get(Type::Int32Ty, Scale);
78717877 if (isa(NumElements))
78727878 Amt = Context->getConstantExprMul(cast(NumElements),
78737879 cast(Amt));
78797885 }
78807886
78817887 if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
7882 Value *Off = Context->getConstantInt(Type::Int32Ty, Offset, true);
7888 Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true);
78837889 Instruction *Tmp = BinaryOperator::CreateAdd(Amt, Off, "tmp");
78847890 Amt = InsertNewInstBefore(Tmp, AI);
78857891 }
81808186 assert((uint64_t)Offset < (uint64_t)TySize && "Out of range offset");
81818187 }
81828188
8183 NewIndices.push_back(Context->getConstantInt(IntPtrTy, FirstIdx));
8189 NewIndices.push_back(ConstantInt::get(IntPtrTy, FirstIdx));
81848190
81858191 // Index into the types. If we fail, set OrigBase to null.
81868192 while (Offset) {
81948200 "Offset must stay within the indexed type");
81958201
81968202 unsigned Elt = SL->getElementContainingOffset(Offset);
8197 NewIndices.push_back(Context->getConstantInt(Type::Int32Ty, Elt));
8203 NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt));
81988204
81998205 Offset -= SL->getElementOffset(Elt);
82008206 Ty = STy->getElementType(Elt);
82018207 } else if (const ArrayType *AT = dyn_cast(Ty)) {
82028208 uint64_t EltSize = TD->getTypeAllocSize(AT->getElementType());
82038209 assert(EltSize && "Cannot index into a zero-sized array");
8204 NewIndices.push_back(Context->getConstantInt(IntPtrTy,Offset/EltSize));
8210 NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize));
82058211 Offset %= EltSize;
82068212 Ty = AT->getElementType();
82078213 } else {
83988404 return ReplaceInstUsesWith(CI, Res);
83998405
84008406 // We need to emit an AND to clear the high bits.
8401 Constant *C = Context->getConstantInt(APInt::getLowBitsSet(DestBitSize,
8402 SrcBitSize));
8407 Constant *C = ConstantInt::get(*Context,
8408 APInt::getLowBitsSet(DestBitSize, SrcBitSize));
84038409 return BinaryOperator::CreateAnd(Res, C);
84048410 }
84058411 case Instruction::SExt: {
84468452 (!Op0->hasOneUse() || !isa(Op0))) {
84478453 Value *New = InsertCastBefore(Instruction::ZExt, Op0, DestTy, CI);
84488454 return BinaryOperator::CreateXor(New,
8449 Context->getConstantInt(CI.getType(), 1));
8455 ConstantInt::get(CI.getType(), 1));
84508456 }
84518457 break;
84528458
84768482
84778483 // Canonicalize trunc x to i1 -> (icmp ne (and x, 1), 0)
84788484 if (DestBitWidth == 1) {
8479 Constant *One = Context->getConstantInt(Src->getType(), 1);
8485 Constant *One = ConstantInt::get(Src->getType(), 1);
84808486 Src = InsertNewInstBefore(BinaryOperator::CreateAnd(Src, One, "tmp"), CI);
84818487 Value *Zero = Context->getNullValue(Src->getType());
84828488 return new ICmpInst(*Context, ICmpInst::ICMP_NE, Src, Zero);
85238529 if (!DoXform) return ICI;
85248530
85258531 Value *In = ICI->getOperand(0);
8526 Value *Sh = Context->getConstantInt(In->getType(),
8532 Value *Sh = ConstantInt::get(In->getType(),
85278533 In->getType()->getScalarSizeInBits()-1);
85288534 In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, Sh,
85298535 In->getName()+".lobit"),
85338539 false/*ZExt*/, "tmp", &CI);
85348540
85358541 if (ICI->getPredicate() == ICmpInst::ICMP_SGT) {
8536 Constant *One = Context->getConstantInt(In->getType(), 1);
8542 Constant *One = ConstantInt::get(In->getType(), 1);
85378543 In = InsertNewInstBefore(BinaryOperator::CreateXor(In, One,
85388544 In->getName()+".not"),
85398545 CI);
85698575 if (Op1CV != 0 && (Op1CV != KnownZeroMask)) {
85708576 // (X&4) == 2 --> false
85718577 // (X&4) != 2 --> true
8572 Constant *Res = Context->getConstantInt(Type::Int1Ty, isNE);
8578 Constant *Res = ConstantInt::get(Type::Int1Ty, isNE);
85738579 Res = Context->getConstantExprZExt(Res, CI.getType());
85748580 return ReplaceInstUsesWith(CI, Res);
85758581 }
85808586 // Perform a logical shr by shiftamt.
85818587 // Insert the shift to put the result in the low bit.
85828588 In = InsertNewInstBefore(BinaryOperator::CreateLShr(In,
8583 Context->getConstantInt(In->getType(), ShiftAmt),
8589 ConstantInt::get(In->getType(), ShiftAmt),
85848590 In->getName()+".lobit"), CI);
85858591 }
85868592
85878593 if ((Op1CV != 0) == isNE) { // Toggle the low bit.
8588 Constant *One = Context->getConstantInt(In->getType(), 1);
8594 Constant *One = ConstantInt::get(In->getType(), 1);
85898595 In = BinaryOperator::CreateXor(In, One, "tmp");
85908596 InsertNewInstBefore(cast(In), CI);
85918597 }
86248630 // SrcSize > DstSize: trunc(a) & mask
86258631 if (SrcSize < DstSize) {
86268632 APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
8627 Constant *AndConst = Context->getConstantInt(A->getType(), AndValue);
8633 Constant *AndConst = ConstantInt::get(A->getType(), AndValue);
86288634 Instruction *And =
86298635 BinaryOperator::CreateAnd(A, AndConst, CSrc->getName()+".mask");
86308636 InsertNewInstBefore(And, CI);
86318637 return new ZExtInst(And, CI.getType());
86328638 } else if (SrcSize == DstSize) {
86338639 APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
8634 return BinaryOperator::CreateAnd(A, Context->getConstantInt(A->getType(),
8640 return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(),
86358641 AndValue));
86368642 } else if (SrcSize > DstSize) {
86378643 Instruction *Trunc = new TruncInst(A, CI.getType(), "tmp");
86388644 InsertNewInstBefore(Trunc, CI);
86398645 APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize));
86408646 return BinaryOperator::CreateAnd(Trunc,
8641 Context->getConstantInt(Trunc->getType(),
8647 ConstantInt::get(Trunc->getType(),
86428648 AndValue));
86438649 }
86448650 }
87538759 unsigned MidSize = Src->getType()->getScalarSizeInBits();
87548760 unsigned SrcDstSize = CI.getType()->getScalarSizeInBits();
87558761 unsigned ShAmt = CA->getZExtValue()+SrcDstSize-MidSize;
8756 Constant *ShAmtV = Context->getConstantInt(CI.getType(), ShAmt);
8762 Constant *ShAmtV = ConstantInt::get(CI.getType(), ShAmt);
87578763 I = InsertNewInstBefore(BinaryOperator::CreateShl(I, ShAmtV,
87588764 CI.getName()), CI);
87598765 return BinaryOperator::CreateAShr(I, ShAmtV);
90889094 case Instruction::And:
90899095 return Context->getAllOnesValue(I->getType());
90909096 case Instruction::Mul:
9091 return Context->getConstantInt(I->getType(), 1);
9097 return ConstantInt::get(I->getType(), 1);
90929098 }
90939099 }
90949100
93219327 if ((Pred == ICmpInst::ICMP_SLT && Op1CV == 0) ||
93229328 (Pred == ICmpInst::ICMP_SGT && Op1CV.isAllOnesValue())) {
93239329 Value *In = ICI->getOperand(0);
9324 Value *Sh = Context->getConstantInt(In->getType(),
9330 Value *Sh = ConstantInt::get(In->getType(),
93259331 In->getType()->getScalarSizeInBits()-1);
93269332 In = InsertNewInstBefore(BinaryOperator::CreateAShr(In, Sh,
93279333 In->getName()+".lobit"),
96739679 unsigned CopyAlign = MI->getAlignment();
96749680
96759681 if (CopyAlign < MinAlign) {
9676 MI->setAlignment(Context->getConstantInt(MI->getAlignmentType(),
9682 MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
96779683 MinAlign, false));
96789684 return MI;
96799685 }
97489754 Instruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) {
97499755 unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest());
97509756 if (MI->getAlignment() < Alignment) {
9751 MI->setAlignment(Context->getConstantInt(MI->getAlignmentType(),
9757 MI->setAlignment(ConstantInt::get(MI->getAlignmentType(),
97529758 Alignment, false));
97539759 return MI;
97549760 }
97769782
97779783 // Extract the fill value and store.
97789784 uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL;
9779 InsertNewInstBefore(new StoreInst(Context->getConstantInt(ITy, Fill),
9785 InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill),
97809786 Dest, false, Alignment), *MI);
97819787
97829788 // Set the size of the copy to 0, it will be deleted on the next iteration.
99499955 if (ExtractedElts[Idx] == 0) {
99509956 Instruction *Elt =
99519957 new ExtractElementInst(Idx < 16 ? Op0 : Op1,
9952 Context->getConstantInt(Type::Int32Ty, Idx&15, false), "tmp");
9958 ConstantInt::get(Type::Int32Ty, Idx&15, false), "tmp");
99539959 InsertNewInstBefore(Elt, CI);
99549960 ExtractedElts[Idx] = Elt;
99559961 }
99569962
99579963 // Insert this value into the result vector.
99589964 Result = InsertElementInst::Create(Result, ExtractedElts[Idx],
9959 Context->getConstantInt(Type::Int32Ty, i, false),
9965 ConstantInt::get(Type::Int32Ty, i, false),
99609966 "tmp");
99619967 InsertNewInstBefore(cast(Result), CI);
99629968 }
1122911235 if (ArrayEltSize == 1) {
1123011236 NewIdx = GEP.getOperand(1);
1123111237 Scale =
11232 Context->getConstantInt(cast(NewIdx->getType()), 1);
11238 ConstantInt::get(cast(NewIdx->getType()), 1);
1123311239 } else if (ConstantInt *CI = dyn_cast(GEP.getOperand(1))) {
11234 NewIdx = Context->getConstantInt(CI->getType(), 1);
11240 NewIdx = ConstantInt::get(CI->getType(), 1);
1123511241 Scale = CI;
1123611242 } else if (Instruction *Inst =dyn_cast(GEP.getOperand(1))){
1123711243 if (Inst->getOpcode() == Instruction::Shl &&
1123811244 isa(Inst->getOperand(1))) {
1123911245 ConstantInt *ShAmt = cast(Inst->getOperand(1));
1124011246 uint32_t ShAmtVal = ShAmt->getLimitedValue(64);
11241 Scale = Context->getConstantInt(cast(Inst->getType()),
11247 Scale = ConstantInt::get(cast(Inst->getType()),
1124211248 1ULL << ShAmtVal);
1124311249 NewIdx = Inst->getOperand(0);
1124411250 } else if (Inst->getOpcode() == Instruction::Mul &&
1125411260 // operation after making sure Scale doesn't have the sign bit set.
1125511261 if (ArrayEltSize && Scale && Scale->getSExtValue() >= 0LL &&
1125611262 Scale->getZExtValue() % ArrayEltSize == 0) {
11257 Scale = Context->getConstantInt(Scale->getType(),
11263 Scale = ConstantInt::get(Scale->getType(),
1125811264 Scale->getZExtValue() / ArrayEltSize);
1125911265 if (Scale->getZExtValue() != 1) {
1126011266 Constant *C =
1146511471 SingleChar = 0;
1146611472 StrVal = (StrVal << 8) | SingleChar;
1146711473 }
11468 Value *NL = Context->getConstantInt(StrVal);
11474 Value *NL = ConstantInt::get(*Context, StrVal);
1146911475 return IC.ReplaceInstUsesWith(LI, NL);
1147011476 }
1147111477 }
1246112467 return ReplaceInstUsesWith(EI, Context->getUndef(EI.getType()));
1246212468 }
1246312469 return new ExtractElementInst(Src,
12464 Context->getConstantInt(Type::Int32Ty, SrcIdx, false));
12470 ConstantInt::get(Type::Int32Ty, SrcIdx, false));
1246512471 }
1246612472 }
1246712473 // FIXME: Canonicalize extractelement(bitcast) -> bitcast(extractelement)
1248412490 return true;
1248512491 } else if (V == LHS) {
1248612492 for (unsigned i = 0; i != NumElts; ++i)
12487 Mask.push_back(Context->getConstantInt(Type::Int32Ty, i));
12493 Mask.push_back(ConstantInt::get(Type::Int32Ty, i));
1248812494 return true;
1248912495 } else if (V == RHS) {
1249012496 for (unsigned i = 0; i != NumElts; ++i)
12491 Mask.push_back(Context->getConstantInt(Type::Int32Ty, i+NumElts));
12497 Mask.push_back(ConstantInt::get(Type::Int32Ty, i+NumElts));
1249212498 return true;
1249312499 } else if (InsertElementInst *IEI = dyn_cast(V)) {
1249412500 // If this is an insert of an extract from some other vector, include it.
1252212528 // If so, update the mask to reflect the inserted value.
1252312529 if (EI->getOperand(0) == LHS) {
1252412530 Mask[InsertedIdx % NumElts] =
12525 Context->getConstantInt(Type::Int32Ty, ExtractedIdx);
12531 ConstantInt::get(Type::Int32Ty, ExtractedIdx);
1252612532 } else {
1252712533 assert(EI->getOperand(0) == RHS);
1252812534 Mask[InsertedIdx % NumElts] =
12529 Context->getConstantInt(Type::Int32Ty, ExtractedIdx+NumElts);
12535 ConstantInt::get(Type::Int32Ty, ExtractedIdx+NumElts);
1253012536
1253112537 }
1253212538 return true;
1255412560 Mask.assign(NumElts, Context->getUndef(Type::Int32Ty));
1255512561 return V;
1255612562 } else if (isa(V)) {
12557 Mask.assign(NumElts, Context->getConstantInt(Type::Int32Ty, 0));
12563 Mask.assign(NumElts, ConstantInt::get(Type::Int32Ty, 0));
1255812564 return V;
1255912565 } else if (InsertElementInst *IEI = dyn_cast(V)) {
1256012566 // If this is an insert of an extract from some other vector, include it.
1257512581 RHS = EI->getOperand(0);
1257612582 Value *V = CollectShuffleElements(VecOp, Mask, RHS, Context);
1257712583 Mask[InsertedIdx % NumElts] =
12578 Context->getConstantInt(Type::Int32Ty, NumElts+ExtractedIdx);
12584 ConstantInt::get(Type::Int32Ty, NumElts+ExtractedIdx);
1257912585 return V;
1258012586 }
1258112587
1258512591 // Everything but the extracted element is replaced with the RHS.
1258612592 for (unsigned i = 0; i != NumElts; ++i) {
1258712593 if (i != InsertedIdx)
12588 Mask[i] = Context->getConstantInt(Type::Int32Ty, NumElts+i);
12594 Mask[i] = ConstantInt::get(Type::Int32Ty, NumElts+i);
1258912595 }
1259012596 return V;
1259112597 }
1260312609
1260412610 // Otherwise, can't do anything fancy. Return an identity vector.
1260512611 for (unsigned i = 0; i != NumElts; ++i)
12606 Mask.push_back(Context->getConstantInt(Type::Int32Ty, i));
12612 Mask.push_back(ConstantInt::get(Type::Int32Ty, i));
1260712613 return V;
1260812614 }
1260912615
1265012656 Mask.assign(NumVectorElts, Context->getUndef(Type::Int32Ty));
1265112657 else {
1265212658 assert(isa(VecOp) && "Unknown thing");
12653 Mask.assign(NumVectorElts, Context->getConstantInt(Type::Int32Ty,
12659 Mask.assign(NumVectorElts, ConstantInt::get(Type::Int32Ty,
1265412660 NumVectorElts));
1265512661 }
1265612662 Mask[InsertedIdx] =
12657 Context->getConstantInt(Type::Int32Ty, ExtractedIdx);
12663 ConstantInt::get(Type::Int32Ty, ExtractedIdx);
1265812664 return new ShuffleVectorInst(EI->getOperand(0), VecOp,
1265912665 Context->getConstantVector(Mask));
1266012666 }
1272712733 Elts.push_back(Context->getUndef(Type::Int32Ty));
1272812734 } else {
1272912735 Mask[i] = Mask[i] % e; // Force to LHS.
12730 Elts.push_back(Context->getConstantInt(Type::Int32Ty, Mask[i]));
12736 Elts.push_back(ConstantInt::get(Type::Int32Ty, Mask[i]));
1273112737 }
1273212738 }
1273312739 }
1278512791 if (NewMask[i] >= LHSInNElts*2) {
1278612792 Elts.push_back(Context->getUndef(Type::Int32Ty));
1278712793 } else {
12788 Elts.push_back(Context->getConstantInt(Type::Int32Ty, NewMask[i]));
12794 Elts.push_back(ConstantInt::get(Type::Int32Ty, NewMask[i]));
1278912795 }
1279012796 }
1279112797 return new ShuffleVectorInst(LHSSVI->getOperand(0),
433433 << "' folding condition to '" << BranchDir << "': "
434434 << *BB->getTerminator();
435435 ++NumFolds;
436 DestBI->setCondition(BB->getContext().getConstantInt(Type::Int1Ty,
437 BranchDir));
436 DestBI->setCondition(ConstantInt::get(Type::Int1Ty, BranchDir));
438437 ConstantFoldTerminator(BB);
439438 return true;
440439 }
756755 // We can only do the simplification for phi nodes of 'false' with AND or
757756 // 'true' with OR. See if we have any entries in the phi for this.
758757 unsigned PredNo = ~0U;
759 ConstantInt *PredCst = V->getContext().getConstantInt(Type::Int1Ty, !isAnd);
758 ConstantInt *PredCst = ConstantInt::get(Type::Int1Ty, !isAnd);
760759 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
761760 if (PN->getIncomingValue(i) == PredCst) {
762761 PredNo = i;
294294 // Return V+1
295295 static Value *getPlusOne(Value *V, bool Sign, Instruction *InsertPt,
296296 LLVMContext &Context) {
297 Constant *One = Context.getConstantInt(V->getType(), 1, Sign);
297 Constant *One = ConstantInt::get(V->getType(), 1, Sign);
298298 return BinaryOperator::CreateAdd(V, One, "lsp", InsertPt);
299299 }
300300
301301 // Return V-1
302302 static Value *getMinusOne(Value *V, bool Sign, Instruction *InsertPt,
303303 LLVMContext &Context) {
304 Constant *One = Context.getConstantInt(V->getType(), 1, Sign);
304 Constant *One = ConstantInt::get(V->getType(), 1, Sign);
305305 return BinaryOperator::CreateSub(V, One, "lsp", InsertPt);
306306 }
307307
19901990
19911991 NewStride = &IU->StrideOrder[i];
19921992 if (!isa(NewCmpTy))
1993 NewCmpRHS = Context.getConstantInt(NewCmpTy, NewCmpVal);
1993 NewCmpRHS = ConstantInt::get(NewCmpTy, NewCmpVal);
19941994 else {
1995 Constant *CI = Context.getConstantInt(NewCmpIntTy, NewCmpVal);
1995 Constant *CI = ConstantInt::get(NewCmpIntTy, NewCmpVal);
19961996 NewCmpRHS = Context.getConstantExprIntToPtr(CI, NewCmpTy);
19971997 }
19981998 NewOffset = TyBits == NewTyBits
24312431 if (!ExitingBlock)
24322432 return; // More than one block exiting!
24332433
2434 LLVMContext &Context = ExitingBlock->getContext();
2435
24362434 // Okay, we've computed the exiting block. See what condition causes us to
24372435 // exit.
24382436 //
25052503 Value *startVal = phi->getIncomingValue(inBlock);
25062504 Value *endVal = Cond->getOperand(1);
25072505 // FIXME check for case where both are constant
2508 Constant* Zero = Context.getConstantInt(Cond->getOperand(1)->getType(), 0);
2506 Constant* Zero = ConstantInt::get(Cond->getOperand(1)->getType(), 0);
25092507 BinaryOperator *NewStartVal =
25102508 BinaryOperator::Create(Instruction::Sub, endVal, startVal,
25112509 "tmp", PreInsertPt);
910910 if (IsEqual)
911911 Replacement = Val;
912912 else
913 Replacement = Context.getConstantInt(Type::Int1Ty,
913 Replacement = ConstantInt::get(Type::Int1Ty,
914914 !cast(Val)->getZExtValue());
915915
916916 for (unsigned i = 0, e = Users.size(); i != e; ++i)
6868 if (Val != Val2)
6969 return 0;
7070 }
71 return Context.getConstantInt(Val);
71 return ConstantInt::get(Context, Val);
7272 }
7373 }
7474
447447 Value *Ops[] = {
448448 StartPtr, ByteVal, // Start, value
449449 // size
450 SI->getContext().getConstantInt(Type::Int64Ty, Range.End-Range.Start),
450 ConstantInt::get(Type::Int64Ty, Range.End-Range.Start),
451451 // align
452 SI->getContext().getConstantInt(Type::Int32Ty, Range.Alignment)
452 ConstantInt::get(Type::Int32Ty, Range.Alignment)
453453 };
454454 Value *C = CallInst::Create(MemSetF, Ops, Ops+4, "", InsertPt);
455455 DEBUG(cerr << "Replace stores:\n";
11681168 Value *V = VN.value(n); // XXX: redesign worklist.
11691169 const Type *Ty = V->getType();
11701170 if (Ty->isInteger()) {
1171 addToWorklist(V, Context->getConstantInt(*I), ICmpInst::ICMP_EQ, VRP);
1171 addToWorklist(V, ConstantInt::get(*Context, *I),
1172 ICmpInst::ICMP_EQ, VRP);
11721173 return;
11731174 } else if (const PointerType *PTy = dyn_cast(Ty)) {
11741175 assert(*I == 0 && "Pointer is null but not zero?");
17821783 if (ConstantInt *CI = dyn_cast(Canonical)) {
17831784 if (ConstantInt *Arg = dyn_cast(LHS)) {
17841785 add(RHS,
1785 Context->getConstantInt(CI->getValue() ^ Arg->getValue()),
1786 ConstantInt::get(*Context, CI->getValue() ^ Arg->getValue()),
17861787 ICmpInst::ICMP_EQ, NewContext);
17871788 }
17881789 }
18941895 assert(!Ty->isFPOrFPVector() && "Float in work queue!");
18951896
18961897 Constant *Zero = Context->getNullValue(Ty);
1897 Constant *One = Context->getConstantInt(Ty, 1);
1898 Constant *One = ConstantInt::get(Ty, 1);
18981899 ConstantInt *AllOnes = cast(Context->getAllOnesValue(Ty));
18991900
19001901 switch (Opcode) {
25342535
25352536 void PredicateSimplifier::Forwards::visitSExtInst(SExtInst &SI) {
25362537 VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &SI);
2537 LLVMContext *Context = &SI.getContext();
2538 LLVMContext &Context = SI.getContext();
25382539 uint32_t SrcBitWidth = cast(SI.getSrcTy())->getBitWidth();
25392540 uint32_t DstBitWidth = cast(SI.getDestTy())->getBitWidth();
25402541 APInt Min(APInt::getHighBitsSet(DstBitWidth, DstBitWidth-SrcBitWidth+1));
25412542 APInt Max(APInt::getLowBitsSet(DstBitWidth, SrcBitWidth-1));
2542 VRP.add(Context->getConstantInt(Min), &SI, ICmpInst::ICMP_SLE);
2543 VRP.add(Context->getConstantInt(Max), &SI, ICmpInst::ICMP_SGE);
2543 VRP.add(ConstantInt::get(Context, Min), &SI, ICmpInst::ICMP_SLE);
2544 VRP.add(ConstantInt::get(Context, Max), &SI, ICmpInst::ICMP_SGE);
25442545 VRP.solve();
25452546 }
25462547
25472548 void PredicateSimplifier::Forwards::visitZExtInst(ZExtInst &ZI) {
25482549 VRPSolver VRP(VN, IG, UB, VR, PS->DTDFS, PS->modified, &ZI);
2549 LLVMContext *Context = &ZI.getContext();
2550 LLVMContext &Context = ZI.getContext();
25502551 uint32_t SrcBitWidth = cast(ZI.getSrcTy())->getBitWidth();
25512552 uint32_t DstBitWidth = cast(ZI.getDestTy())->getBitWidth();
25522553 APInt Max(APInt::getLowBitsSet(DstBitWidth, SrcBitWidth));
2553 VRP.add(Context->getConstantInt(Max), &ZI, ICmpInst::ICMP_UGE);
2554 VRP.add(ConstantInt::get(Context, Max), &ZI, ICmpInst::ICMP_UGE);
25542555 VRP.solve();
25552556 }
25562557
26392640
26402641 Pred = IC.getPredicate();
26412642
2642 LLVMContext *Context = &IC.getContext();
2643 LLVMContext &Context = IC.getContext();
26432644
26442645 if (ConstantInt *Op1 = dyn_cast(IC.getOperand(1))) {
26452646 ConstantInt *NextVal = 0;
26482649 case ICmpInst::ICMP_SLT:
26492650 case ICmpInst::ICMP_ULT:
26502651 if (Op1->getValue() != 0)
2651 NextVal = Context->getConstantInt(Op1->getValue()-1);
2652 NextVal = ConstantInt::get(Context, Op1->getValue()-1);
26522653 break;
26532654 case ICmpInst::ICMP_SGT:
26542655 case ICmpInst::ICMP_UGT:
26552656 if (!Op1->getValue().isAllOnesValue())
2656 NextVal = Context->getConstantInt(Op1->getValue()+1);
2657 NextVal = ConstantInt::get(Context, Op1->getValue()+1);
26572658 break;
26582659 }
26592660
465465 (Shl->hasOneUse() &&
466466 (isReassociableOp(Shl->use_back(), Instruction::Mul) ||
467467 isReassociableOp(Shl->use_back(), Instruction::Add)))) {
468 Constant *MulCst = Context.getConstantInt(Shl->getType(), 1);
468 Constant *MulCst = ConstantInt::get(Shl->getType(), 1);
469469 MulCst =
470470 Context.getConstantExprShl(MulCst, cast(Shl->getOperand(1)));
471471
775775 unsigned OtherEltAlign = MemAlignment;
776776
777777 if (OtherPtr) {
778 Value *Idx[2] = { Zero, Context.getConstantInt(Type::Int32Ty, i) };
778 Value *Idx[2] = { Zero, ConstantInt::get(Type::Int32Ty, i) };
779779 OtherElt = GetElementPtrInst::Create(OtherPtr, Idx, Idx + 2,
780780 OtherPtr->getNameStr()+"."+utostr(i),
781781 MI);
838838 }
839839
840840 // Convert the integer value to the appropriate type.
841 StoreVal = Context.getConstantInt(TotalVal);
841 StoreVal = ConstantInt::get(Context, TotalVal);
842842 if (isa(ValTy))
843843 StoreVal = Context.getConstantExprIntToPtr(StoreVal, ValTy);
844844 else if (ValTy->isFloatingPoint())
875875 Value *Ops[] = {
876876 SROADest ? EltPtr : OtherElt, // Dest ptr
877877 SROADest ? OtherElt : EltPtr, // Src ptr
878 Context.getConstantInt(MI->getOperand(3)->getType(), EltSize), // Size
879 Context.getConstantInt(Type::Int32Ty, OtherEltAlign) // Align
878 ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size
879 ConstantInt::get(Type::Int32Ty, OtherEltAlign) // Align
880880 };
881881 CallInst::Create(TheFn, Ops, Ops + 4, "", MI);
882882 } else {
883883 assert(isa(MI));
884884 Value *Ops[] = {
885885 EltPtr, MI->getOperand(2), // Dest, Value,
886 Context.getConstantInt(MI->getOperand(3)->getType(), EltSize), // Size
886 ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size
887887 Zero // Align
888888 };
889889 CallInst::Create(TheFn, Ops, Ops + 4, "", MI);
933933
934934 Value *EltVal = SrcVal;
935935 if (Shift) {
936 Value *ShiftVal = Context.getConstantInt(EltVal->getType(), Shift);
936 Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift);
937937 EltVal = BinaryOperator::CreateLShr(EltVal, ShiftVal,
938938 "sroa.store.elt", SI);
939939 }
981981
982982 Value *EltVal = SrcVal;
983983 if (Shift) {
984 Value *ShiftVal = Context.getConstantInt(EltVal->getType(), Shift);
984 Value *ShiftVal = ConstantInt::get(EltVal->getType(), Shift);
985985 EltVal = BinaryOperator::CreateLShr(EltVal, ShiftVal,
986986 "sroa.store.elt", SI);
987987 }
10881088 Shift = AllocaSizeBits-Shift-FieldIntTy->getBitWidth();
10891089
10901090 if (Shift) {
1091 Value *ShiftVal = Context.getConstantInt(SrcField->getType(), Shift);
1091 Value *ShiftVal = ConstantInt::get(SrcField->getType(), Shift);
10921092 SrcField = BinaryOperator::CreateShl(SrcField, ShiftVal, "", LI);
10931093 }
10941094
12111211 Indices.begin(),
12121212 Indices.end(),
12131213 GEPI->getName()+".0", GEPI);
1214 Indices[1] = Context.getConstantInt(Type::Int32Ty, 1);
1214 Indices[1] = ConstantInt::get(Type::Int32Ty, 1);
12151215 Value *OneIdx = GetElementPtrInst::Create(GEPI->getOperand(0),
12161216 Indices.begin(),
12171217 Indices.end(),
14681468
14691469 Value *Old = Builder.CreateLoad(NewAI, (NewAI->getName()+".in").c_str());
14701470 Value *New = ConvertScalar_InsertValue(
1471 User->getContext().getConstantInt(APVal),
1471 ConstantInt::get(User->getContext(), APVal),
14721472 Old, Offset, Builder);
14731473 Builder.CreateStore(New, NewAI);
14741474 }
15571557 }
15581558 // Return the element extracted out of it.
15591559 Value *V = Builder.CreateExtractElement(FromVal,
1560 Context.getConstantInt(Type::Int32Ty,Elt),
1560 ConstantInt::get(Type::Int32Ty,Elt),
15611561 "tmp");
15621562 if (V->getType() != ToType)
15631563 V = Builder.CreateBitCast(V, ToType, "tmp");
16101610 // only some bits are used.
16111611 if (ShAmt > 0 && (unsigned)ShAmt < NTy->getBitWidth())
16121612 FromVal = Builder.CreateLShr(FromVal,
1613 Context.getConstantInt(FromVal->getType(),
1613 ConstantInt::get(FromVal->getType(),
16141614 ShAmt), "tmp");
16151615 else if (ShAmt < 0 && (unsigned)-ShAmt < NTy->getBitWidth())
16161616 FromVal = Builder.CreateShl(FromVal,
1617 Context.getConstantInt(FromVal->getType(),
1617 ConstantInt::get(FromVal->getType(),
16181618 -ShAmt), "tmp");
16191619
16201620 // Finally, unconditionally truncate the integer to the right width.
16761676 SV = Builder.CreateBitCast(SV, VTy->getElementType(), "tmp");
16771677
16781678 SV = Builder.CreateInsertElement(Old, SV,
1679 Context.getConstantInt(Type::Int32Ty, Elt),
1679 ConstantInt::get(Type::Int32Ty, Elt),
16801680 "tmp");
16811681 return SV;
16821682 }
17441744 // only some bits in the structure are set.
17451745 APInt Mask(APInt::getLowBitsSet(DestWidth, SrcWidth));
17461746 if (ShAmt > 0 && (unsigned)ShAmt < DestWidth) {
1747 SV = Builder.CreateShl(SV, Context.getConstantInt(SV->getType(),
1747 SV = Builder.CreateShl(SV, ConstantInt::get(SV->getType(),
17481748 ShAmt), "tmp");
17491749 Mask <<= ShAmt;
17501750 } else if (ShAmt < 0 && (unsigned)-ShAmt < DestWidth) {
1751 SV = Builder.CreateLShr(SV, Context.getConstantInt(SV->getType(),
1751 SV = Builder.CreateLShr(SV, ConstantInt::get(SV->getType(),
17521752 -ShAmt), "tmp");
17531753 Mask = Mask.lshr(-ShAmt);
17541754 }
17571757 // in the new bits.
17581758 if (SrcWidth != DestWidth) {
17591759 assert(DestWidth > SrcWidth);
1760 Old = Builder.CreateAnd(Old, Context.getConstantInt(~Mask), "mask");
1760 Old = Builder.CreateAnd(Old, ConstantInt::get(Context, ~Mask), "mask");
17611761 SV = Builder.CreateOr(Old, SV, "ins");
17621762 }
17631763 return SV;
156156 Tys[0] = Len->getType();
157157 Value *MemCpy = Intrinsic::getDeclaration(M, IID, Tys, 1);
158158 return B.CreateCall4(MemCpy, CastToCStr(Dst, B), CastToCStr(Src, B), Len,
159 Context->getConstantInt(Type::Int32Ty, Align));
159 ConstantInt::get(Type::Int32Ty, Align));
160160 }
161161
162162 /// EmitMemChr - Emit a call to the memchr function. This assumes that Ptr is
212212 const Type *Tys[1];
213213 Tys[0] = Len->getType();
214214 Value *MemSet = Intrinsic::getDeclaration(M, IID, Tys, 1);
215 Value *Align = Context->getConstantInt(Type::Int32Ty, 1);
215 Value *Align = ConstantInt::get(Type::Int32Ty, 1);
216216 return B.CreateCall4(MemSet, CastToCStr(Dst, B), Val, Len, Align);
217217 }
218218
345345 TD->getIntPtrType(), TD->getIntPtrType(),
346346 File->getType(), NULL);
347347 CallInst *CI = B.CreateCall4(F, CastToCStr(Ptr, B), Size,
348 Context->getConstantInt(TD->getIntPtrType(), 1), File);
348 ConstantInt::get(TD->getIntPtrType(), 1), File);
349349
350350 if (const Function *Fn = dyn_cast(F->stripPointerCasts()))
351351 CI->setCallingConv(Fn->getCallingConv());
579579 // We have enough information to now generate the memcpy call to do the
580580 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
581581 EmitMemCpy(CpyDst, Src,
582 Context->getConstantInt(TD->getIntPtrType(), Len+1), 1, B);
582 ConstantInt::get(TD->getIntPtrType(), Len+1), 1, B);
583583 }
584584 };
585585
651651 return 0;
652652
653653 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
654 Context->getConstantInt(TD->getIntPtrType(), Len), B);
654 ConstantInt::get(TD->getIntPtrType(), Len), B);
655655 }
656656
657657 // Otherwise, the character is a constant, see if the first argument is
676676 }
677677
678678 // strchr(s+n,c) -> gep(s+n+i,c)
679 Value *Idx = Context->getConstantInt(Type::Int64Ty, i);
679 Value *Idx = ConstantInt::get(Type::Int64Ty, i);
680680 return B.CreateGEP(SrcStr, Idx, "strchr");
681681 }
682682 };
695695
696696 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
697697 if (Str1P == Str2P) // strcmp(x,x) -> 0
698 return Context->getConstantInt(CI->getType(), 0);
698 return ConstantInt::get(CI->getType(), 0);
699699
700700 std::string Str1, Str2;
701701 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
709709
710710 // strcmp(x, y) -> cnst (if both x and y are constant strings)
711711 if (HasStr1 && HasStr2)
712 return Context->getConstantInt(CI->getType(),
712 return ConstantInt::get(CI->getType(),
713713 strcmp(Str1.c_str(),Str2.c_str()));
714714
715715 // strcmp(P, "x") -> memcmp(P, "x", 2)
717717 uint64_t Len2 = GetStringLength(Str2P);
718718 if (Len1 && Len2) {
719719 return EmitMemCmp(Str1P, Str2P,
720 Context->getConstantInt(TD->getIntPtrType(),
720 ConstantInt::get(TD->getIntPtrType(),
721721 std::min(Len1, Len2)), B);
722722 }
723723
740740
741741 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
742742 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
743 return Context->getConstantInt(CI->getType(), 0);
743 return ConstantInt::get(CI->getType(), 0);
744744
745745 // Get the length argument if it is constant.
746746 uint64_t Length;
750750 return 0;
751751
752752 if (Length == 0) // strncmp(x,y,0) -> 0
753 return Context->getConstantInt(CI->getType(), 0);
753 return ConstantInt::get(CI->getType(), 0);
754754
755755 std::string Str1, Str2;
756756 bool HasStr1 = GetConstantStringInfo(Str1P, Str1);
764764
765765 // strncmp(x, y) -> cnst (if both x and y are constant strings)
766766 if (HasStr1 && HasStr2)
767 return Context->getConstantInt(CI->getType(),
767 return ConstantInt::get(CI->getType(),
768768 strncmp(Str1.c_str(), Str2.c_str(), Length));
769769 return 0;
770770 }
794794 // We have enough information to now generate the memcpy call to do the
795795 // concatenation for us. Make a memcpy to copy the nul byte with align = 1.
796796 EmitMemCpy(Dst, Src,
797 Context->getConstantInt(TD->getIntPtrType(), Len), 1, B);
797 ConstantInt::get(TD->getIntPtrType(), Len), 1, B);
798798 return Dst;
799799 }
800800 };
822822
823823 if (SrcLen == 0) {
824824 // strncpy(x, "", y) -> memset(x, '\0', y, 1)
825 EmitMemSet(Dst, Context->getConstantInt(Type::Int8Ty, '\0'), LenOp, B);
825 EmitMemSet(Dst, ConstantInt::get(Type::Int8Ty, '\0'), LenOp, B);
826826 return Dst;
827827 }
828828
839839
840840 // strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
841841 EmitMemCpy(Dst, Src,
842 Context->getConstantInt(TD->getIntPtrType(), Len), 1, B);
842 ConstantInt::get(TD->getIntPtrType(), Len), 1, B);
843843
844844 return Dst;
845845 }
860860
861861 // Constant folding: strlen("xyz") -> 3
862862 if (uint64_t Len = GetStringLength(Src))
863 return Context->getConstantInt(CI->getType(), Len-1);
863 return ConstantInt::get(CI->getType(), Len-1);
864864
865865 // Handle strlen(p) != 0.
866866 if (!IsOnlyUsedInZeroEqualityComparison(CI)) return 0;
979979 Value *Dst = CastToCStr(CI->getOperand(1), B);
980980 Value *Src = CastToCStr(CI->getOperand(2), B);
981981 Value *Size = CI->getOperand(3);
982 Value *Align = Context->getConstantInt(Type::Int32Ty, 1);
982 Value *Align = ConstantInt::get(Type::Int32Ty, 1);
983983 B.CreateCall4(MemMove, Dst, Src, Size, Align);
984984 return CI->getOperand(1);
985985 }
11531153 if (ConstantInt *CI = dyn_cast(Op)) {
11541154 if (CI->getValue() == 0) // ffs(0) -> 0.
11551155 return Context->getNullValue(CI->getType());
1156 return Context->getConstantInt(Type::Int32Ty, // ffs(c) -> cttz(c)+1
1156 return ConstantInt::get(Type::Int32Ty, // ffs(c) -> cttz(c)+1
11571157 CI->getValue().countTrailingZeros()+1);
11581158 }
11591159
11621162 Value *F = Intrinsic::getDeclaration(Callee->getParent(),
11631163 Intrinsic::cttz, &ArgType, 1);
11641164 Value *V = B.CreateCall(F, Op, "cttz");
1165 V = B.CreateAdd(V, Context->getConstantInt(V->getType(), 1), "tmp");
1165 V = B.CreateAdd(V, ConstantInt::get(V->getType(), 1), "tmp");
11661166 V = B.CreateIntCast(V, Type::Int32Ty, false, "tmp");
11671167
11681168 Value *Cond = B.CreateICmpNE(Op, Context->getNullValue(ArgType), "tmp");
1169 return B.CreateSelect(Cond, V, Context->getConstantInt(Type::Int32Ty, 0));
1169 return B.CreateSelect(Cond, V, ConstantInt::get(Type::Int32Ty, 0));
11701170 }
11711171 };
11721172
11831183
11841184 // isdigit(c) -> (c-'0')
11851185 Value *Op = CI->getOperand(1);
1186 Op = B.CreateSub(Op, Context->getConstantInt(Type::Int32Ty, '0'),
1186 Op = B.CreateSub(Op, ConstantInt::get(Type::Int32Ty, '0'),
11871187 "isdigittmp");
1188 Op = B.CreateICmpULT(Op, Context->getConstantInt(Type::Int32Ty, 10),
1188 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::Int32Ty, 10),
11891189 "isdigit");
11901190 return B.CreateZExt(Op, CI->getType());
11911191 }
12041204
12051205 // isascii(c) -> c
12061206 Value *Op = CI->getOperand(1);
1207 Op = B.CreateICmpULT(Op, Context->getConstantInt(Type::Int32Ty, 128),
1207 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::Int32Ty, 128),
12081208 "isascii");
12091209 return B.CreateZExt(Op, CI->getType());
12101210 }
12451245
12461246 // isascii(c) -> c & 0x7f
12471247 return B.CreateAnd(CI->getOperand(1),
1248 Context->getConstantInt(CI->getType(),0x7F));
1248 ConstantInt::get(CI->getType(),0x7F));
12491249 }
12501250 };
12511251
12731273 // Empty format string -> noop.
12741274 if (FormatStr.empty()) // Tolerate printf's declared void.
12751275 return CI->use_empty() ? (Value*)CI :
1276 Context->getConstantInt(CI->getType(), 0);
1276 ConstantInt::get(CI->getType(), 0);
12771277
12781278 // printf("x") -> putchar('x'), even for '%'.
12791279 if (FormatStr.size() == 1) {
1280 EmitPutChar(Context->getConstantInt(Type::Int32Ty, FormatStr[0]), B);
1280 EmitPutChar(ConstantInt::get(Type::Int32Ty, FormatStr[0]), B);
12811281 return CI->use_empty() ? (Value*)CI :
1282 Context->getConstantInt(CI->getType(), 1);
1282 ConstantInt::get(CI->getType(), 1);
12831283 }
12841284
12851285 // printf("foo\n") --> puts("foo")
12931293 GlobalVariable::InternalLinkage, C, "str");
12941294 EmitPutS(C, B);
12951295 return CI->use_empty() ? (Value*)CI :
1296 Context->getConstantInt(CI->getType(), FormatStr.size()+1);
1296 ConstantInt::get(CI->getType(), FormatStr.size()+1);
12971297 }
12981298
12991299 // Optimize specific format strings.
13021302 isa(CI->getOperand(2)->getType())) {
13031303 EmitPutChar(CI->getOperand(2), B);
13041304 return CI->use_empty() ? (Value*)CI :
1305 Context->getConstantInt(CI->getType(), 1);
1305 ConstantInt::get(CI->getType(), 1);
13061306 }
13071307
13081308 // printf("%s\n", str) --> puts(str)
13431343
13441344 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
13451345 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
1346 Context->getConstantInt(TD->getIntPtrType(), FormatStr.size()+1),1,B);
1347 return Context->getConstantInt(CI->getType(), FormatStr.size());
1346 ConstantInt::get(TD->getIntPtrType(), FormatStr.size()+1),1,B);
1347 return ConstantInt::get(CI->getType(), FormatStr.size());
13481348 }
13491349
13501350 // The remaining optimizations require the format string to be "%s" or "%c"
13591359 Value *V = B.CreateTrunc(CI->getOperand(3), Type::Int8Ty, "char");
13601360 Value *Ptr = CastToCStr(CI->getOperand(1), B);
13611361 B.CreateStore(V, Ptr);
1362 Ptr = B.CreateGEP(Ptr, Context->getConstantInt(Type::Int32Ty, 1), "nul");
1362 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::Int32Ty, 1), "nul");
13631363 B.CreateStore(Context->getNullValue(Type::Int8Ty), Ptr);
13641364
1365 return Context->getConstantInt(CI->getType(), 1);
1365 return ConstantInt::get(CI->getType(), 1);
13661366 }
13671367
13681368 if (FormatStr[1] == 's') {
13711371
13721372 Value *Len = EmitStrLen(CI->getOperand(3), B);
13731373 Value *IncLen = B.CreateAdd(Len,
1374 Context->getConstantInt(Len->getType(), 1),
1374 ConstantInt::get(Len->getType(), 1),
13751375 "leninc");
13761376 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B);
13771377
14041404
14051405 // If this is writing zero records, remove the call (it's a noop).
14061406 if (Bytes == 0)
1407 return Context->getConstantInt(CI->getType(), 0);
1407 return ConstantInt::get(CI->getType(), 0);
14081408
14091409 // If this is writing one byte, turn it into fputc.
14101410 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
14111411 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
14121412 EmitFPutC(Char, CI->getOperand(4), B);
1413 return Context->getConstantInt(CI->getType(), 1);
1413 return ConstantInt::get(CI->getType(), 1);
14141414 }
14151415
14161416 return 0;
14331433 uint64_t Len = GetStringLength(CI->getOperand(1));
14341434 if (!Len) return 0;
14351435 EmitFWrite(CI->getOperand(1),
1436 Context->getConstantInt(TD->getIntPtrType(), Len-1),
1436 ConstantInt::get(TD->getIntPtrType(), Len-1),
14371437 CI->getOperand(2), B);
14381438 return CI; // Known to have no uses (see above).
14391439 }
14621462 if (FormatStr[i] == '%') // Could handle %% -> % if we cared.
14631463 return 0; // We found a format specifier.
14641464
1465 EmitFWrite(CI->getOperand(2), Context->getConstantInt(TD->getIntPtrType(),
1465 EmitFWrite(CI->getOperand(2), ConstantInt::get(TD->getIntPtrType(),
14661466 FormatStr.size()),
14671467 CI->getOperand(1), B);
1468 return Context->getConstantInt(CI->getType(), FormatStr.size());
1468 return ConstantInt::get(CI->getType(), FormatStr.size());
14691469 }
14701470
14711471 // The remaining optimizations require the format string to be "%s" or "%c"
14781478 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
14791479 if (!isa(CI->getOperand(3)->getType())) return 0;
14801480 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B);
1481 return Context->getConstantInt(CI->getType(), 1);
1481 return ConstantInt::get(CI->getType(), 1);
14821482 }
14831483
14841484 if (FormatStr[1] == 's') {
305305 if (AggregateArgs) {
306306 Value *Idx[2];
307307 Idx[0] = Context.getNullValue(Type::Int32Ty);
308 Idx[1] = Context.getConstantInt(Type::Int32Ty, i);
308 Idx[1] = ConstantInt::get(Type::Int32Ty, i);
309309 TerminatorInst *TI = newFunction->begin()->getTerminator();
310310 GetElementPtrInst *GEP =
311311 GetElementPtrInst::Create(AI, Idx, Idx+2,
395395 for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
396396 Value *Idx[2];
397397 Idx[0] = Context.getNullValue(Type::Int32Ty);
398 Idx[1] = Context.getConstantInt(Type::Int32Ty, i);
398 Idx[1] = ConstantInt::get(Type::Int32Ty, i);
399399 GetElementPtrInst *GEP =
400400 GetElementPtrInst::Create(Struct, Idx, Idx + 2,
401401 "gep_" + StructValues[i]->getName());
421421 if (AggregateArgs) {
422422 Value *Idx[2];
423423 Idx[0] = Context.getNullValue(Type::Int32Ty);
424 Idx[1] = Context.getConstantInt(Type::Int32Ty, FirstOut + i);
424 Idx[1] = ConstantInt::get(Type::Int32Ty, FirstOut + i);
425425 GetElementPtrInst *GEP
426426 = GetElementPtrInst::Create(Struct, Idx, Idx + 2,
427427 "gep_reload_" + outputs[i]->getName());
473473 case 0:
474474 case 1: break; // No value needed.
475475 case 2: // Conditional branch, return a bool
476 brVal = Context.getConstantInt(Type::Int1Ty, !SuccNum);
476 brVal = ConstantInt::get(Type::Int1Ty, !SuccNum);
477477 break;
478478 default:
479 brVal = Context.getConstantInt(Type::Int16Ty, SuccNum);
479 brVal = ConstantInt::get(Type::Int16Ty, SuccNum);
480480 break;
481481 }
482482
483483 ReturnInst *NTRet = ReturnInst::Create(brVal, NewTarget);
484484
485485 // Update the switch instruction.
486 TheSwitch->addCase(Context.getConstantInt(Type::Int16Ty, SuccNum),
486 TheSwitch->addCase(ConstantInt::get(Type::Int16Ty, SuccNum),
487487 OldTarget);
488488
489489 // Restore values just before we exit
522522 if (AggregateArgs) {
523523