llvm.org GIT mirror llvm / 7cbd8a3
API change for {BinaryOperator|CmpInst|CastInst}::create*() --> Create. Legacy interfaces will be in place for some time. (Merge from use-diet branch.) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@51200 91177308-0d34-0410-b5e6-96231b3b80d8 Gabor Greif 11 years ago
31 changed file(s) with 642 addition(s) and 549 deletion(s). Raw diff Collapse all Expand all
6565 ReturnInst::Create(One, RetBB);
6666
6767 // create fib(x-1)
68 Value *Sub = BinaryOperator::createSub(ArgX, One, "arg", RecurseBB);
68 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
6969 CallInst *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
7070 CallFibX1->setTailCall();
7171
7272 // create fib(x-2)
73 Sub = BinaryOperator::createSub(ArgX, Two, "arg", RecurseBB);
73 Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
7474 CallInst *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
7575 CallFibX2->setTailCall();
7676
7777
7878 // fib(x-1)+fib(x-2)
79 Value *Sum = BinaryOperator::createAdd(CallFibX1, CallFibX2,
79 Value *Sum = BinaryOperator::CreateAdd(CallFibX1, CallFibX2,
8080 "addresult", RecurseBB);
8181
8282 // Create the return instruction and add it to the basic block
6868 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
6969
7070 // Create the add instruction, inserting it into the end of BB.
71 Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
71 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
7272
7373 // Create the return instruction and add it to the basic block
7474 ReturnInst::Create(Add, BB);
4242 Value *Three = ConstantInt::get(Type::Int32Ty, 3);
4343
4444 // Create the add instruction... does not insert...
45 Instruction *Add = BinaryOperator::create(Instruction::Add, Two, Three,
45 Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
4646 "addresult");
4747
4848 // explicitly insert it into the basic block...
4949 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
5050
5151 // Create the add instruction, inserting it into the end of BB.
52 Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
52 Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
5353
5454 // Create the return instruction and add it to the basic block
5555 ReturnInst::Create(Add, BB);
8989 ReturnInst::Create(One, RetBB);
9090
9191 // create fib(x-1)
92 Value *Sub = BinaryOperator::createSub(ArgX, One, "arg", RecurseBB);
92 Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
9393 Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
9494
9595 // create fib(x-2)
96 Sub = BinaryOperator::createSub(ArgX, Two, "arg", RecurseBB);
96 Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
9797 Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
9898
9999 // fib(x-1)+fib(x-2)
100100 Value *Sum =
101 BinaryOperator::createAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
101 BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
102102
103103 // Create the return instruction and add it to the basic block
104104 ReturnInst::Create(Sum, RecurseBB);
100100
101101 Value *visitTruncateExpr(SCEVTruncateExpr *S) {
102102 Value *V = expand(S->getOperand());
103 return CastInst::createTruncOrBitCast(V, S->getType(), "tmp.", InsertPt);
103 return CastInst::CreateTruncOrBitCast(V, S->getType(), "tmp.", InsertPt);
104104 }
105105
106106 Value *visitZeroExtendExpr(SCEVZeroExtendExpr *S) {
107107 Value *V = expand(S->getOperand());
108 return CastInst::createZExtOrBitCast(V, S->getType(), "tmp.", InsertPt);
108 return CastInst::CreateZExtOrBitCast(V, S->getType(), "tmp.", InsertPt);
109109 }
110110
111111 Value *visitSignExtendExpr(SCEVSignExtendExpr *S) {
112112 Value *V = expand(S->getOperand());
113 return CastInst::createSExtOrBitCast(V, S->getType(), "tmp.", InsertPt);
113 return CastInst::CreateSExtOrBitCast(V, S->getType(), "tmp.", InsertPt);
114114 }
115115
116116 Value *visitAddExpr(SCEVAddExpr *S) {
151151 /// Transparently provide more efficient getOperand methods.
152152 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
153153
154 /// create() - Construct a binary instruction, given the opcode and the two
154 /// Create() - Construct a binary instruction, given the opcode and the two
155155 /// operands. Optionally (if InstBefore is specified) insert the instruction
156156 /// into a BasicBlock right before the specified instruction. The specified
157157 /// Instruction is allowed to be a dereferenced end iterator.
158158 ///
159 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
159 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
160160 const std::string &Name = "",
161161 Instruction *InsertBefore = 0);
162162
163 /// create() - Construct a binary instruction, given the opcode and the two
163 /// Create() - Construct a binary instruction, given the opcode and the two
164164 /// operands. Also automatically insert this instruction to the end of the
165165 /// BasicBlock specified.
166166 ///
167 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
167 static BinaryOperator *Create(BinaryOps Op, Value *S1, Value *S2,
168168 const std::string &Name,
169169 BasicBlock *InsertAtEnd);
170170
171 /// create* - These methods just forward to create, and are useful when you
171 /// Create* - These methods just forward to create, and are useful when you
172172 /// statically know what type of instruction you're going to create. These
173173 /// helpers just save some typing.
174174 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
175 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
175 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
176176 const std::string &Name = "") {\
177 return create(Instruction::OPC, V1, V2, Name);\
177 return Create(Instruction::OPC, V1, V2, Name);\
178178 }
179179 #include "llvm/Instruction.def"
180180 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
181 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
181 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
182182 const std::string &Name, BasicBlock *BB) {\
183 return create(Instruction::OPC, V1, V2, Name, BB);\
183 return Create(Instruction::OPC, V1, V2, Name, BB);\
184184 }
185185 #include "llvm/Instruction.def"
186186 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
187 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
187 static BinaryOperator *Create##OPC(Value *V1, Value *V2, \
188188 const std::string &Name, Instruction *I) {\
189 return create(Instruction::OPC, V1, V2, Name, I);\
189 return Create(Instruction::OPC, V1, V2, Name, I);\
190190 }
191191 #include "llvm/Instruction.def"
192192
194194 /// Helper functions to construct and inspect unary operations (NEG and NOT)
195195 /// via binary operators SUB and XOR:
196196 ///
197 /// createNeg, createNot - Create the NEG and NOT
197 /// CreateNeg, CreateNot - Create the NEG and NOT
198198 /// instructions out of SUB and XOR instructions.
199199 ///
200 static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
200 static BinaryOperator *CreateNeg(Value *Op, const std::string &Name = "",
201201 Instruction *InsertBefore = 0);
202 static BinaryOperator *createNeg(Value *Op, const std::string &Name,
202 static BinaryOperator *CreateNeg(Value *Op, const std::string &Name,
203203 BasicBlock *InsertAtEnd);
204 static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
204 static BinaryOperator *CreateNot(Value *Op, const std::string &Name = "",
205205 Instruction *InsertBefore = 0);
206 static BinaryOperator *createNot(Value *Op, const std::string &Name,
206 static BinaryOperator *CreateNot(Value *Op, const std::string &Name,
207207 BasicBlock *InsertAtEnd);
208208
209209 /// isNeg, isNot - Check if the given Value is a NEG or NOT instruction.
239239 }
240240 static inline bool classof(const Value *V) {
241241 return isa(V) && classof(cast(V));
242 }
243
244 /// Backward-compatible interfaces
245 /// @deprecated in 2.4, do not use, will disappear soon
246 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
247 const std::string &Name = "",
248 Instruction *InsertBefore = 0) {
249 return Create(Op, S1, S2, Name, InsertBefore);
250 }
251 static BinaryOperator *create(BinaryOps Op, Value *S1, Value *S2,
252 const std::string &Name,
253 BasicBlock *InsertAtEnd) {
254 return Create(Op, S1, S2, Name, InsertAtEnd);
255 }
256 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
257 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
258 const std::string &Name = "") {\
259 return Create(Instruction::OPC, V1, V2, Name);\
260 }
261 #include "llvm/Instruction.def"
262 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
263 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
264 const std::string &Name, BasicBlock *BB) {\
265 return Create(Instruction::OPC, V1, V2, Name, BB);\
266 }
267 #include "llvm/Instruction.def"
268 #define HANDLE_BINARY_INST(N, OPC, CLASS) \
269 static BinaryOperator *create##OPC(Value *V1, Value *V2, \
270 const std::string &Name, Instruction *I) {\
271 return Create(Instruction::OPC, V1, V2, Name, I);\
272 }
273 #include "llvm/Instruction.def"
274 static BinaryOperator *createNeg(Value *Op, const std::string &Name = "",
275 Instruction *InsertBefore = 0) {
276 return CreateNeg(Op, Name, InsertBefore);
277 }
278 static BinaryOperator *createNeg(Value *Op, const std::string &Name,
279 BasicBlock *InsertAtEnd) {
280 return CreateNeg(Op, Name, InsertAtEnd);
281 }
282 static BinaryOperator *createNot(Value *Op, const std::string &Name = "",
283 Instruction *InsertBefore = 0) {
284 return CreateNot(Op, Name, InsertBefore);
285 }
286 static BinaryOperator *createNot(Value *Op, const std::string &Name,
287 BasicBlock *InsertAtEnd) {
288 return CreateNot(Op, Name, InsertAtEnd);
242289 }
243290 };
244291
285332 /// constructor has insert-before-instruction semantics to automatically
286333 /// insert the new CastInst before InsertBefore (if it is non-null).
287334 /// @brief Construct any of the CastInst subclasses
288 static CastInst *create(
335 static CastInst *Create(
289336 Instruction::CastOps, ///< The opcode of the cast instruction
290337 Value *S, ///< The value to be casted (operand 0)
291338 const Type *Ty, ///< The type to which cast should be made
298345 /// to automatically insert the new CastInst at the end of InsertAtEnd (if
299346 /// its non-null).
300347 /// @brief Construct any of the CastInst subclasses
301 static CastInst *create(
348 static CastInst *Create(
302349 Instruction::CastOps, ///< The opcode for the cast instruction
303350 Value *S, ///< The value to be casted (operand 0)
304351 const Type *Ty, ///< The type to which operand is casted
307354 );
308355
309356 /// @brief Create a ZExt or BitCast cast instruction
310 static CastInst *createZExtOrBitCast(
357 static CastInst *CreateZExtOrBitCast(
311358 Value *S, ///< The value to be casted (operand 0)
312359 const Type *Ty, ///< The type to which cast should be made
313360 const std::string &Name = "", ///< Name for the instruction
315362 );
316363
317364 /// @brief Create a ZExt or BitCast cast instruction
318 static CastInst *createZExtOrBitCast(
365 static CastInst *CreateZExtOrBitCast(
319366 Value *S, ///< The value to be casted (operand 0)
320367 const Type *Ty, ///< The type to which operand is casted
321368 const std::string &Name, ///< The name for the instruction
323370 );
324371
325372 /// @brief Create a SExt or BitCast cast instruction
326 static CastInst *createSExtOrBitCast(
373 static CastInst *CreateSExtOrBitCast(
327374 Value *S, ///< The value to be casted (operand 0)
328375 const Type *Ty, ///< The type to which cast should be made
329376 const std::string &Name = "", ///< Name for the instruction
330377 Instruction *InsertBefore = 0 ///< Place to insert the instruction
331378 );
332379
380 /// @brief Create a SExt or BitCast cast instruction
381 static CastInst *CreateSExtOrBitCast(
382 Value *S, ///< The value to be casted (operand 0)
383 const Type *Ty, ///< The type to which operand is casted
384 const std::string &Name, ///< The name for the instruction
385 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
386 );
387
333388 /// @brief Create a BitCast or a PtrToInt cast instruction
334 static CastInst *createPointerCast(
389 static CastInst *CreatePointerCast(
335390 Value *S, ///< The pointer value to be casted (operand 0)
336391 const Type *Ty, ///< The type to which operand is casted
337392 const std::string &Name, ///< The name for the instruction
339394 );
340395
341396 /// @brief Create a BitCast or a PtrToInt cast instruction
342 static CastInst *createPointerCast(
397 static CastInst *CreatePointerCast(
343398 Value *S, ///< The pointer value to be casted (operand 0)
344399 const Type *Ty, ///< The type to which cast should be made
345400 const std::string &Name = "", ///< Name for the instruction
347402 );
348403
349404 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
350 static CastInst *createIntegerCast(
405 static CastInst *CreateIntegerCast(
351406 Value *S, ///< The pointer value to be casted (operand 0)
352407 const Type *Ty, ///< The type to which cast should be made
353408 bool isSigned, ///< Whether to regard S as signed or not
356411 );
357412
358413 /// @brief Create a ZExt, BitCast, or Trunc for int -> int casts.
359 static CastInst *createIntegerCast(
414 static CastInst *CreateIntegerCast(
360415 Value *S, ///< The integer value to be casted (operand 0)
361416 const Type *Ty, ///< The integer type to which operand is casted
362417 bool isSigned, ///< Whether to regard S as signed or not
365420 );
366421
367422 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
368 static CastInst *createFPCast(
423 static CastInst *CreateFPCast(
369424 Value *S, ///< The floating point value to be casted
370425 const Type *Ty, ///< The floating point type to cast to
371426 const std::string &Name = "", ///< Name for the instruction
373428 );
374429
375430 /// @brief Create an FPExt, BitCast, or FPTrunc for fp -> fp casts
376 static CastInst *createFPCast(
431 static CastInst *CreateFPCast(
377432 Value *S, ///< The floating point value to be casted
378433 const Type *Ty, ///< The floating point type to cast to
379434 const std::string &Name, ///< The name for the instruction
380435 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
381436 );
382437
383 /// @brief Create a SExt or BitCast cast instruction
384 static CastInst *createSExtOrBitCast(
385 Value *S, ///< The value to be casted (operand 0)
386 const Type *Ty, ///< The type to which operand is casted
387 const std::string &Name, ///< The name for the instruction
388 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
389 );
390
391438 /// @brief Create a Trunc or BitCast cast instruction
392 static CastInst *createTruncOrBitCast(
439 static CastInst *CreateTruncOrBitCast(
393440 Value *S, ///< The value to be casted (operand 0)
394441 const Type *Ty, ///< The type to which cast should be made
395442 const std::string &Name = "", ///< Name for the instruction
397444 );
398445
399446 /// @brief Create a Trunc or BitCast cast instruction
400 static CastInst *createTruncOrBitCast(
447 static CastInst *CreateTruncOrBitCast(
401448 Value *S, ///< The value to be casted (operand 0)
402449 const Type *Ty, ///< The type to which operand is casted
403450 const std::string &Name, ///< The name for the instruction
485532 }
486533 static inline bool classof(const Value *V) {
487534 return isa(V) && classof(cast(V));
535 }
536 /// Backward-compatible interfaces
537 /// @deprecated in 2.4, do not use, will disappear soon
538 static CastInst *create(Instruction::CastOps Op,Value *S,const Type *Ty,
539 const std::string &Name = "",Instruction *InsertBefore = 0) {
540 return Create(Op,S,Ty,Name,InsertBefore);
541 }
542 static CastInst *create(Instruction::CastOps Op,Value *S,const Type *Ty,
543 const std::string &Name,BasicBlock *InsertAtEnd) {
544 return Create(Op,S,Ty,Name,InsertAtEnd);
545 }
546
547 #define DEFINE_CASTINST_DEPRECATED(OP) \
548 static CastInst *create ## OP ## Cast(Value *S, const Type *Ty, \
549 const std::string &Name = "", Instruction *InsertBefore = 0) { \
550 return Create ## OP ## Cast(S, Ty, Name, InsertBefore); \
551 } \
552 static CastInst *create ## OP ## Cast(Value *S, const Type *Ty, \
553 const std::string &Name, BasicBlock *InsertAtEnd) { \
554 return Create ## OP ## Cast(S, Ty, Name, InsertAtEnd); \
555 }
556 DEFINE_CASTINST_DEPRECATED(ZExtOrBit)
557 DEFINE_CASTINST_DEPRECATED(SExtOrBit)
558 DEFINE_CASTINST_DEPRECATED(Pointer)
559 DEFINE_CASTINST_DEPRECATED(FP)
560 DEFINE_CASTINST_DEPRECATED(TruncOrBit)
561 #undef DEFINE_CASTINST_DEPRECATED
562 static CastInst *createIntegerCast(Value *S, const Type *Ty, bool isSigned,
563 const std::string &Name = "", Instruction *InsertBefore = 0) {
564 return CreateIntegerCast(S, Ty, isSigned, Name, InsertBefore);
565 }
566 static CastInst *createIntegerCast(Value *S, const Type *Ty, bool isSigned,
567 const std::string &Name, BasicBlock *InsertAtEnd) {
568 return CreateIntegerCast(S, Ty, isSigned, Name, InsertAtEnd);
488569 }
489570 };
490571
557638 /// instruction into a BasicBlock right before the specified instruction.
558639 /// The specified Instruction is allowed to be a dereferenced end iterator.
559640 /// @brief Create a CmpInst
560 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
641 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
561642 Value *S2, const std::string &Name = "",
562643 Instruction *InsertBefore = 0);
563644
565646 /// two operands. Also automatically insert this instruction to the end of
566647 /// the BasicBlock specified.
567648 /// @brief Create a CmpInst
568 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
649 static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1,
569650 Value *S2, const std::string &Name,
570651 BasicBlock *InsertAtEnd);
571652
626707 static inline bool classof(const Value *V) {
627708 return isa(V) && classof(cast(V));
628709 }
710 /// Backward-compatible interfaces
711 /// @deprecated in 2.4, do not use, will disappear soon
712 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
713 Value *S2, const std::string &Name = "",
714 Instruction *InsertBefore = 0) {
715 return Create(Op, predicate, S1, S2, Name, InsertBefore);
716 }
717 static CmpInst *create(OtherOps Op, unsigned short predicate, Value *S1,
718 Value *S2, const std::string &Name,
719 BasicBlock *InsertAtEnd) {
720 return Create(Op, predicate, S1, S2, Name, InsertAtEnd);
721 }
629722 };
630723
631724
148148 if (Constant *LC = dyn_cast(LHS))
149149 if (Constant *RC = dyn_cast(RHS))
150150 return ConstantExpr::getAdd(LC, RC);
151 return Insert(BinaryOperator::createAdd(LHS, RHS, Name));
151 return Insert(BinaryOperator::CreateAdd(LHS, RHS, Name));
152152 }
153153 Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
154154 if (Constant *LC = dyn_cast(LHS))
155155 if (Constant *RC = dyn_cast(RHS))
156156 return ConstantExpr::getSub(LC, RC);
157 return Insert(BinaryOperator::createSub(LHS, RHS, Name));
157 return Insert(BinaryOperator::CreateSub(LHS, RHS, Name));
158158 }
159159 Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
160160 if (Constant *LC = dyn_cast(LHS))
161161 if (Constant *RC = dyn_cast(RHS))
162162 return ConstantExpr::getMul(LC, RC);
163 return Insert(BinaryOperator::createMul(LHS, RHS, Name));
163 return Insert(BinaryOperator::CreateMul(LHS, RHS, Name));
164164 }
165165 Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
166166 if (Constant *LC = dyn_cast(LHS))
167167 if (Constant *RC = dyn_cast(RHS))
168168 return ConstantExpr::getUDiv(LC, RC);
169 return Insert(BinaryOperator::createUDiv(LHS, RHS, Name));
169 return Insert(BinaryOperator::CreateUDiv(LHS, RHS, Name));
170170 }
171171 Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
172172 if (Constant *LC = dyn_cast(LHS))
173173 if (Constant *RC = dyn_cast(RHS))
174174 return ConstantExpr::getSDiv(LC, RC);
175 return Insert(BinaryOperator::createSDiv(LHS, RHS, Name));
175 return Insert(BinaryOperator::CreateSDiv(LHS, RHS, Name));
176176 }
177177 Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
178178 if (Constant *LC = dyn_cast(LHS))
179179 if (Constant *RC = dyn_cast(RHS))
180180 return ConstantExpr::getFDiv(LC, RC);
181 return Insert(BinaryOperator::createFDiv(LHS, RHS, Name));
181 return Insert(BinaryOperator::CreateFDiv(LHS, RHS, Name));
182182 }
183183 Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
184184 if (Constant *LC = dyn_cast(LHS))
185185 if (Constant *RC = dyn_cast(RHS))
186186 return ConstantExpr::getURem(LC, RC);
187 return Insert(BinaryOperator::createURem(LHS, RHS, Name));
187 return Insert(BinaryOperator::CreateURem(LHS, RHS, Name));
188188 }
189189 Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
190190 if (Constant *LC = dyn_cast(LHS))
191191 if (Constant *RC = dyn_cast(RHS))
192192 return ConstantExpr::getSRem(LC, RC);
193 return Insert(BinaryOperator::createSRem(LHS, RHS, Name));
193 return Insert(BinaryOperator::CreateSRem(LHS, RHS, Name));
194194 }
195195 Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
196196 if (Constant *LC = dyn_cast(LHS))
197197 if (Constant *RC = dyn_cast(RHS))
198198 return ConstantExpr::getFRem(LC, RC);
199 return Insert(BinaryOperator::createFRem(LHS, RHS, Name));
199 return Insert(BinaryOperator::CreateFRem(LHS, RHS, Name));
200200 }
201201 Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
202202 if (Constant *LC = dyn_cast(LHS))
203203 if (Constant *RC = dyn_cast(RHS))
204204 return ConstantExpr::getShl(LC, RC);
205 return Insert(BinaryOperator::createShl(LHS, RHS, Name));
205 return Insert(BinaryOperator::CreateShl(LHS, RHS, Name));
206206 }
207207 Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
208208 if (Constant *LC = dyn_cast(LHS))
209209 if (Constant *RC = dyn_cast(RHS))
210210 return ConstantExpr::getLShr(LC, RC);
211 return Insert(BinaryOperator::createLShr(LHS, RHS, Name));
211 return Insert(BinaryOperator::CreateLShr(LHS, RHS, Name));
212212 }
213213 Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
214214 if (Constant *LC = dyn_cast(LHS))
215215 if (Constant *RC = dyn_cast(RHS))
216216 return ConstantExpr::getAShr(LC, RC);
217 return Insert(BinaryOperator::createAShr(LHS, RHS, Name));
217 return Insert(BinaryOperator::CreateAShr(LHS, RHS, Name));
218218 }
219219 Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
220220 if (Constant *LC = dyn_cast(LHS))
221221 if (Constant *RC = dyn_cast(RHS))
222222 return ConstantExpr::getAnd(LC, RC);
223 return Insert(BinaryOperator::createAnd(LHS, RHS, Name));
223 return Insert(BinaryOperator::CreateAnd(LHS, RHS, Name));
224224 }
225225 Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
226226 if (Constant *LC = dyn_cast(LHS))
227227 if (Constant *RC = dyn_cast(RHS))
228228 return ConstantExpr::getOr(LC, RC);
229 return Insert(BinaryOperator::createOr(LHS, RHS, Name));
229 return Insert(BinaryOperator::CreateOr(LHS, RHS, Name));
230230 }
231231 Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
232232 if (Constant *LC = dyn_cast(LHS))
233233 if (Constant *RC = dyn_cast(RHS))
234234 return ConstantExpr::getXor(LC, RC);
235 return Insert(BinaryOperator::createXor(LHS, RHS, Name));
235 return Insert(BinaryOperator::CreateXor(LHS, RHS, Name));
236236 }
237237
238238 BinaryOperator *CreateBinOp(Instruction::BinaryOps Opc,
239239 Value *LHS, Value *RHS, const char *Name = "") {
240 return Insert(BinaryOperator::create(Opc, LHS, RHS, Name));
240 return Insert(BinaryOperator::Create(Opc, LHS, RHS, Name));
241241 }
242242
243243 BinaryOperator *CreateNeg(Value *V, const char *Name = "") {
244 return Insert(BinaryOperator::createNeg(V, Name));
244 return Insert(BinaryOperator::CreateNeg(V, Name));
245245 }
246246 BinaryOperator *CreateNot(Value *V, const char *Name = "") {
247 return Insert(BinaryOperator::createNot(V, Name));
247 return Insert(BinaryOperator::CreateNot(V, Name));
248248 }
249249
250250 //===--------------------------------------------------------------------===//
356356 return V;
357357 if (Constant *VC = dyn_cast(V))
358358 return ConstantExpr::getCast(Op, VC, DestTy);
359 return Insert(CastInst::create(Op, V, DestTy, Name));
359 return Insert(CastInst::Create(Op, V, DestTy, Name));
360360 }
361361 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
362362 const char *Name = "") {
364364 return V;
365365 if (Constant *VC = dyn_cast(V))
366366 return ConstantExpr::getIntegerCast(VC, DestTy, isSigned);
367 return Insert(CastInst::createIntegerCast(V, DestTy, isSigned, Name));
367 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned, Name));
368368 }
369369
370370 //===--------------------------------------------------------------------===//
3939 return CI;
4040 }
4141 }
42 return CastInst::create(opcode, V, Ty, V->getName(),
42 return CastInst::Create(opcode, V, Ty, V->getName(),
4343 A->getParent()->getEntryBlock().begin());
4444 }
4545
6666 if (InvokeInst *II = dyn_cast(I))
6767 IP = II->getNormalDest()->begin();
6868 while (isa(IP)) ++IP;
69 return CastInst::create(opcode, V, Ty, V->getName(), IP);
69 return CastInst::Create(opcode, V, Ty, V->getName(), IP);
7070 }
7171
7272 /// InsertBinop - Insert the specified binary operator, doing a small amount
9595 }
9696
9797 // If we don't have
98 return BinaryOperator::create(Opcode, LHS, RHS, "tmp", InsertPt);
98 return BinaryOperator::Create(Opcode, LHS, RHS, "tmp", InsertPt);
9999 }
100100
101101 Value *SCEVExpander::visitMulExpr(SCEVMulExpr *S) {
154154 // Insert a unit add instruction right before the terminator corresponding
155155 // to the back-edge.
156156 Constant *One = ConstantInt::get(Ty, 1);
157 Instruction *Add = BinaryOperator::createAdd(PN, One, "indvar.next",
157 Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next",
158158 (*HPI)->getTerminator());
159159
160160 pred_iterator PI = pred_begin(Header);
28512851 CHECK_FOR_ERROR
28522852 Value* val2 = getVal(*$2, $5);
28532853 CHECK_FOR_ERROR
2854 $$ = BinaryOperator::create($1, val1, val2);
2854 $$ = BinaryOperator::Create($1, val1, val2);
28552855 if ($$ == 0)
28562856 GEN_ERROR("binary operator returned null");
28572857 delete $2;
28682868 CHECK_FOR_ERROR
28692869 Value* tmpVal2 = getVal(*$2, $5);
28702870 CHECK_FOR_ERROR
2871 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
2871 $$ = BinaryOperator::Create($1, tmpVal1, tmpVal2);
28722872 if ($$ == 0)
28732873 GEN_ERROR("binary operator returned null");
28742874 delete $2;
28822882 CHECK_FOR_ERROR
28832883 Value* tmpVal2 = getVal(*$3, $6);
28842884 CHECK_FOR_ERROR
2885 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2885 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
28862886 if ($$ == 0)
28872887 GEN_ERROR("icmp operator returned null");
28882888 delete $3;
28962896 CHECK_FOR_ERROR
28972897 Value* tmpVal2 = getVal(*$3, $6);
28982898 CHECK_FOR_ERROR
2899 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2899 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
29002900 if ($$ == 0)
29012901 GEN_ERROR("fcmp operator returned null");
29022902 delete $3;
29102910 CHECK_FOR_ERROR
29112911 Value* tmpVal2 = getVal(*$3, $6);
29122912 CHECK_FOR_ERROR
2913 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2913 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
29142914 if ($$ == 0)
29152915 GEN_ERROR("icmp operator returned null");
29162916 delete $3;
29242924 CHECK_FOR_ERROR
29252925 Value* tmpVal2 = getVal(*$3, $6);
29262926 CHECK_FOR_ERROR
2927 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2927 $$ = CmpInst::Create($1, $2, tmpVal1, tmpVal2);
29282928 if ($$ == 0)
29292929 GEN_ERROR("fcmp operator returned null");
29302930 delete $3;
29382938 GEN_ERROR("invalid cast opcode for cast from '" +
29392939 Val->getType()->getDescription() + "' to '" +
29402940 DestTy->getDescription() + "'");
2941 $$ = CastInst::create($1, Val, DestTy);
2941 $$ = CastInst::Create($1, Val, DestTy);
29422942 delete $4;
29432943 }
29442944 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
12651265
12661266 int Opc = GetDecodedBinaryOpcode(Record[OpNum], LHS->getType());
12671267 if (Opc == -1) return Error("Invalid BINOP record");
1268 I = BinaryOperator::create((Instruction::BinaryOps)Opc, LHS, RHS);
1268 I = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
12691269 break;
12701270 }
12711271 case bitc::FUNC_CODE_INST_CAST: { // CAST: [opval, opty, destty, castopc]
12791279 int Opc = GetDecodedCastOpcode(Record[OpNum+1]);
12801280 if (Opc == -1 || ResTy == 0)
12811281 return Error("Invalid CAST record");
1282 I = CastInst::create((Instruction::CastOps)Opc, Op, ResTy);
1282 I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
12831283 break;
12841284 }
12851285 case bitc::FUNC_CODE_INST_GEP: { // GEP: [n x operands]
172172 switch(BitSize) {
173173 default: assert(0 && "Unhandled type size of value to byteswap!");
174174 case 16: {
175 Value *Tmp1 = BinaryOperator::createShl(V,
175 Value *Tmp1 = BinaryOperator::CreateShl(V,
176176 ConstantInt::get(V->getType(),8),"bswap.2",IP);
177 Value *Tmp2 = BinaryOperator::createLShr(V,
177 Value *Tmp2 = BinaryOperator::CreateLShr(V,
178178 ConstantInt::get(V->getType(),8),"bswap.1",IP);
179 V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP);
179 V = BinaryOperator::CreateOr(Tmp1, Tmp2, "bswap.i16", IP);
180180 break;
181181 }
182182 case 32: {
183 Value *Tmp4 = BinaryOperator::createShl(V,
183 Value *Tmp4 = BinaryOperator::CreateShl(V,
184184 ConstantInt::get(V->getType(),24),"bswap.4", IP);
185 Value *Tmp3 = BinaryOperator::createShl(V,
185 Value *Tmp3 = BinaryOperator::CreateShl(V,
186186 ConstantInt::get(V->getType(),8),"bswap.3",IP);
187 Value *Tmp2 = BinaryOperator::createLShr(V,
187 Value *Tmp2 = BinaryOperator::CreateLShr(V,
188188 ConstantInt::get(V->getType(),8),"bswap.2",IP);
189 Value *Tmp1 = BinaryOperator::createLShr(V,
189 Value *Tmp1 = BinaryOperator::CreateLShr(V,
190190 ConstantInt::get(V->getType(),24),"bswap.1", IP);
191 Tmp3 = BinaryOperator::createAnd(Tmp3,
191 Tmp3 = BinaryOperator::CreateAnd(Tmp3,
192192 ConstantInt::get(Type::Int32Ty, 0xFF0000),
193193 "bswap.and3", IP);
194 Tmp2 = BinaryOperator::createAnd(Tmp2,
194 Tmp2 = BinaryOperator::CreateAnd(Tmp2,
195195 ConstantInt::get(Type::Int32Ty, 0xFF00),
196196 "bswap.and2", IP);
197 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP);
198 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP);
199 V = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.i32", IP);
197 Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp3, "bswap.or1", IP);
198 Tmp2 = BinaryOperator::CreateOr(Tmp2, Tmp1, "bswap.or2", IP);
199 V = BinaryOperator::CreateOr(Tmp4, Tmp2, "bswap.i32", IP);
200200 break;
201201 }
202202 case 64: {
203 Value *Tmp8 = BinaryOperator::createShl(V,
203 Value *Tmp8 = BinaryOperator::CreateShl(V,
204204 ConstantInt::get(V->getType(),56),"bswap.8", IP);
205 Value *Tmp7 = BinaryOperator::createShl(V,
205 Value *Tmp7 = BinaryOperator::CreateShl(V,
206206 ConstantInt::get(V->getType(),40),"bswap.7", IP);
207 Value *Tmp6 = BinaryOperator::createShl(V,
207 Value *Tmp6 = BinaryOperator::CreateShl(V,
208208 ConstantInt::get(V->getType(),24),"bswap.6", IP);
209 Value *Tmp5 = BinaryOperator::createShl(V,
209 Value *Tmp5 = BinaryOperator::CreateShl(V,
210210 ConstantInt::get(V->getType(),8),"bswap.5", IP);
211 Value* Tmp4 = BinaryOperator::createLShr(V,
211 Value* Tmp4 = BinaryOperator::CreateLShr(V,
212212 ConstantInt::get(V->getType(),8),"bswap.4", IP);
213 Value* Tmp3 = BinaryOperator::createLShr(V,
213 Value* Tmp3 = BinaryOperator::CreateLShr(V,
214214 ConstantInt::get(V->getType(),24),"bswap.3", IP);
215 Value* Tmp2 = BinaryOperator::createLShr(V,
215 Value* Tmp2 = BinaryOperator::CreateLShr(V,
216216 ConstantInt::get(V->getType(),40),"bswap.2", IP);
217 Value* Tmp1 = BinaryOperator::createLShr(V,
217 Value* Tmp1 = BinaryOperator::CreateLShr(V,
218218 ConstantInt::get(V->getType(),56),"bswap.1", IP);
219 Tmp7 = BinaryOperator::createAnd(Tmp7,
219 Tmp7 = BinaryOperator::CreateAnd(Tmp7,
220220 ConstantInt::get(Type::Int64Ty,
221221 0xFF000000000000ULL),
222222 "bswap.and7", IP);
223 Tmp6 = BinaryOperator::createAnd(Tmp6,
223 Tmp6 = BinaryOperator::CreateAnd(Tmp6,
224224 ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL),
225225 "bswap.and6", IP);
226 Tmp5 = BinaryOperator::createAnd(Tmp5,
226 Tmp5 = BinaryOperator::CreateAnd(Tmp5,
227227 ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL),
228228 "bswap.and5", IP);
229 Tmp4 = BinaryOperator::createAnd(Tmp4,
229 Tmp4 = BinaryOperator::CreateAnd(Tmp4,
230230 ConstantInt::get(Type::Int64Ty, 0xFF000000ULL),
231231 "bswap.and4", IP);
232 Tmp3 = BinaryOperator::createAnd(Tmp3,
232 Tmp3 = BinaryOperator::CreateAnd(Tmp3,
233233 ConstantInt::get(Type::Int64Ty, 0xFF0000ULL),
234234 "bswap.and3", IP);
235 Tmp2 = BinaryOperator::createAnd(Tmp2,
235 Tmp2 = BinaryOperator::CreateAnd(Tmp2,
236236 ConstantInt::get(Type::Int64Ty, 0xFF00ULL),
237237 "bswap.and2", IP);
238 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP);
239 Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP);
240 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP);
241 Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP);
242 Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP);
243 Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP);
244 V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP);
238 Tmp8 = BinaryOperator::CreateOr(Tmp8, Tmp7, "bswap.or1", IP);
239 Tmp6 = BinaryOperator::CreateOr(Tmp6, Tmp5, "bswap.or2", IP);
240 Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp3, "bswap.or3", IP);
241 Tmp2 = BinaryOperator::CreateOr(Tmp2, Tmp1, "bswap.or4", IP);
242 Tmp8 = BinaryOperator::CreateOr(Tmp8, Tmp6, "bswap.or5", IP);
243 Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp2, "bswap.or6", IP);
244 V = BinaryOperator::CreateOr(Tmp8, Tmp4, "bswap.i64", IP);
245245 break;
246246 }
247247 }
268268 for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize);
269269 i <<= 1, ++ct) {
270270 Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]);
271 Value *LHS = BinaryOperator::createAnd(
271 Value *LHS = BinaryOperator::CreateAnd(
272272 PartValue, MaskCst, "cppop.and1", IP);
273 Value *VShift = BinaryOperator::createLShr(PartValue,
273 Value *VShift = BinaryOperator::CreateLShr(PartValue,
274274 ConstantInt::get(V->getType(), i), "ctpop.sh", IP);
275 Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP);
276 PartValue = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP);
275 Value *RHS = BinaryOperator::CreateAnd(VShift, MaskCst, "cppop.and2", IP);
276 PartValue = BinaryOperator::CreateAdd(LHS, RHS, "ctpop.step", IP);
277277 }
278 Count = BinaryOperator::createAdd(PartValue, Count, "ctpop.part", IP);
278 Count = BinaryOperator::CreateAdd(PartValue, Count, "ctpop.part", IP);
279279 if (BitSize > 64) {
280 V = BinaryOperator::createLShr(V, ConstantInt::get(V->getType(), 64),
280 V = BinaryOperator::CreateLShr(V, ConstantInt::get(V->getType(), 64),
281281 "ctpop.part.sh", IP);
282282 BitSize -= 64;
283283 }
293293 unsigned BitSize = V->getType()->getPrimitiveSizeInBits();
294294 for (unsigned i = 1; i < BitSize; i <<= 1) {
295295 Value *ShVal = ConstantInt::get(V->getType(), i);
296 ShVal = BinaryOperator::createLShr(V, ShVal, "ctlz.sh", IP);
297 V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP);
298 }
299
300 V = BinaryOperator::createNot(V, "", IP);
296 ShVal = BinaryOperator::CreateLShr(V, ShVal, "ctlz.sh", IP);
297 V = BinaryOperator::CreateOr(V, ShVal, "ctlz.step", IP);
298 }
299
300 V = BinaryOperator::CreateNot(V, "", IP);
301301 return LowerCTPOP(V, IP);
302302 }
303303
354354
355355 // Cast Hi and Lo to the size of Val so the widths are all the same
356356 if (Hi->getType() != Val->getType())
357 Hi = CastInst::createIntegerCast(Hi, Val->getType(), false,
357 Hi = CastInst::CreateIntegerCast(Hi, Val->getType(), false,
358358 "tmp", CurBB);
359359 if (Lo->getType() != Val->getType())
360 Lo = CastInst::createIntegerCast(Lo, Val->getType(), false,
360 Lo = CastInst::CreateIntegerCast(Lo, Val->getType(), false,
361361 "tmp", CurBB);
362362
363363 // Compute a few things that both cases will need, up front.
372372
373373 // First, copmute the number of bits in the forward case.
374374 Instruction* FBitSize =
375 BinaryOperator::createSub(Hi, Lo,"fbits", FwdSize);
375 BinaryOperator::CreateSub(Hi, Lo,"fbits", FwdSize);
376376 BranchInst::Create(Compute, FwdSize);
377377
378378 // Second, compute the number of bits in the reverse case.
379379 Instruction* RBitSize =
380 BinaryOperator::createSub(Lo, Hi, "rbits", RevSize);
380 BinaryOperator::CreateSub(Lo, Hi, "rbits", RevSize);
381381 BranchInst::Create(Compute, RevSize);
382382
383383 // Now, compute the bit range. Start by getting the bitsize and the shift
401401
402402 // Increment the bit size
403403 Instruction *BitSizePlusOne =
404 BinaryOperator::createAdd(BitSize, One, "bits", Compute);
404 BinaryOperator::CreateAdd(BitSize, One, "bits", Compute);
405405
406406 // Create a Mask to zero out the high order bits.
407407 Instruction* Mask =
408 BinaryOperator::createShl(AllOnes, BitSizePlusOne, "mask", Compute);
409 Mask = BinaryOperator::createNot(Mask, "mask", Compute);
408 BinaryOperator::CreateShl(AllOnes, BitSizePlusOne, "mask", Compute);
409 Mask = BinaryOperator::CreateNot(Mask, "mask", Compute);
410410
411411 // Shift the bits down and apply the mask
412412 Instruction* FRes =
413 BinaryOperator::createLShr(Val, ShiftAmt, "fres", Compute);
414 FRes = BinaryOperator::createAnd(FRes, Mask, "fres", Compute);
413 BinaryOperator::CreateLShr(Val, ShiftAmt, "fres", Compute);
414 FRes = BinaryOperator::CreateAnd(FRes, Mask, "fres", Compute);
415415 BranchInst::Create(Reverse, RsltBlk, Cmp, Compute);
416416
417417 // In the Reverse block we have the mask already in FRes but we must reverse
434434 RRes->addIncoming(Zero, Compute);
435435
436436 // Decrement the counter
437 Instruction *Decr = BinaryOperator::createSub(Count, One, "decr", Reverse);
437 Instruction *Decr = BinaryOperator::CreateSub(Count, One, "decr", Reverse);
438438 Count->addIncoming(Decr, Reverse);
439439
440440 // Compute the Bit that we want to move
441441 Instruction *Bit =
442 BinaryOperator::createAnd(BitsToShift, One, "bit", Reverse);
442 BinaryOperator::CreateAnd(BitsToShift, One, "bit", Reverse);
443443
444444 // Compute the new value for next iteration.
445445 Instruction *NewVal =
446 BinaryOperator::createLShr(BitsToShift, One, "rshift", Reverse);
446 BinaryOperator::CreateLShr(BitsToShift, One, "rshift", Reverse);
447447 BitsToShift->addIncoming(NewVal, Reverse);
448448
449449 // Shift the bit into the low bits of the result.
450450 Instruction *NewRes =
451 BinaryOperator::createShl(RRes, One, "lshift", Reverse);
452 NewRes = BinaryOperator::createOr(NewRes, Bit, "addbit", Reverse);
451 BinaryOperator::CreateShl(RRes, One, "lshift", Reverse);
452 NewRes = BinaryOperator::CreateOr(NewRes, Bit, "addbit", Reverse);
453453 RRes->addIncoming(NewRes, Reverse);
454454
455455 // Terminate loop if we've moved all the bits.
542542 new ICmpInst(ICmpInst::ICMP_ULT, Lo, Hi, "", entry);
543543 SelectInst* Hi_pn = SelectInst::Create(is_forward, Hi, Lo, "", entry);
544544 SelectInst* Lo_pn = SelectInst::Create(is_forward, Lo, Hi, "", entry);
545 BinaryOperator* NumBits = BinaryOperator::createSub(Hi_pn, Lo_pn, "",entry);
546 NumBits = BinaryOperator::createAdd(NumBits, One, "", entry);
545 BinaryOperator* NumBits = BinaryOperator::CreateSub(Hi_pn, Lo_pn, "",entry);
546 NumBits = BinaryOperator::CreateAdd(NumBits, One, "", entry);
547547 // Now, convert Lo and Hi to ValTy bit width
548548 if (ValBits > 32) {
549549 Lo = new ZExtInst(Lo_pn, ValTy, "", entry);
558558
559559 // BASIC BLOCK: large
560560 Instruction* MaskBits =
561 BinaryOperator::createSub(RepBitWidth, NumBits, "", large);
562 MaskBits = CastInst::createIntegerCast(MaskBits, RepMask->getType(),
561 BinaryOperator::CreateSub(RepBitWidth, NumBits, "", large);
562 MaskBits = CastInst::CreateIntegerCast(MaskBits, RepMask->getType(),
563563 false, "", large);
564564 BinaryOperator* Mask1 =
565 BinaryOperator::createLShr(RepMask, MaskBits, "", large);
566 BinaryOperator* Rep2 = BinaryOperator::createAnd(Mask1, Rep, "", large);
565 BinaryOperator::CreateLShr(RepMask, MaskBits, "", large);
566 BinaryOperator* Rep2 = BinaryOperator::CreateAnd(Mask1, Rep, "", large);
567567 BranchInst::Create(small, large);
568568
569569 // BASIC BLOCK: small
597597 RRes->addIncoming(ValZero, small);
598598
599599 // Decrement the loop counter by one
600 Instruction *Decr = BinaryOperator::createSub(Count, One, "", reverse);
600 Instruction *Decr = BinaryOperator::CreateSub(Count, One, "", reverse);
601601 Count->addIncoming(Decr, reverse);
602602
603603 // Get the bit that we want to move into the result
604 Value *Bit = BinaryOperator::createAnd(BitsToShift, ValOne, "", reverse);
604 Value *Bit = BinaryOperator::CreateAnd(BitsToShift, ValOne, "", reverse);
605605
606606 // Compute the new value of the bits to shift for the next iteration.
607 Value *NewVal = BinaryOperator::createLShr(BitsToShift, ValOne,"", reverse);
607 Value *NewVal = BinaryOperator::CreateLShr(BitsToShift, ValOne,"", reverse);
608608 BitsToShift->addIncoming(NewVal, reverse);
609609
610610 // Shift the bit we extracted into the low bit of the result.
611 Instruction *NewRes = BinaryOperator::createShl(RRes, ValOne, "", reverse);
612 NewRes = BinaryOperator::createOr(NewRes, Bit, "", reverse);
611 Instruction *NewRes = BinaryOperator::CreateShl(RRes, ValOne, "", reverse);
612 NewRes = BinaryOperator::CreateOr(NewRes, Bit, "", reverse);
613613 RRes->addIncoming(NewRes, reverse);
614614
615615 // Terminate loop if we've moved all the bits.
621621 Rplcmnt->reserveOperandSpace(2);
622622 Rplcmnt->addIncoming(NewRes, reverse);
623623 Rplcmnt->addIncoming(Rep4, small);
624 Value* t0 = CastInst::createIntegerCast(NumBits,ValTy,false,"",result);
625 Value* t1 = BinaryOperator::createShl(ValMask, Lo, "", result);
626 Value* t2 = BinaryOperator::createNot(t1, "", result);
627 Value* t3 = BinaryOperator::createShl(t1, t0, "", result);
628 Value* t4 = BinaryOperator::createOr(t2, t3, "", result);
629 Value* t5 = BinaryOperator::createAnd(t4, Val, "", result);
630 Value* t6 = BinaryOperator::createShl(Rplcmnt, Lo, "", result);
631 Value* Rslt = BinaryOperator::createOr(t5, t6, "part_set", result);
624 Value* t0 = CastInst::CreateIntegerCast(NumBits,ValTy,false,"",result);
625 Value* t1 = BinaryOperator::CreateShl(ValMask, Lo, "", result);
626 Value* t2 = BinaryOperator::CreateNot(t1, "", result);
627 Value* t3 = BinaryOperator::CreateShl(t1, t0, "", result);
628 Value* t4 = BinaryOperator::CreateOr(t2, t3, "", result);
629 Value* t5 = BinaryOperator::CreateAnd(t4, Val, "", result);
630 Value* t6 = BinaryOperator::CreateShl(Rplcmnt, Lo, "", result);
631 Value* Rslt = BinaryOperator::CreateOr(t5, t6, "part_set", result);
632632 ReturnInst::Create(Rslt, result);
633633 }
634634
703703 case Intrinsic::cttz: {
704704 // cttz(x) -> ctpop(~X & (X-1))
705705 Value *Src = CI->getOperand(1);
706 Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI);
706 Value *NotSrc = BinaryOperator::CreateNot(Src, Src->getName()+".not", CI);
707707 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
708 SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI);
709 Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI);
708 SrcM1 = BinaryOperator::CreateSub(Src, SrcM1, "", CI);
709 Src = LowerCTPOP(BinaryOperator::CreateAnd(NotSrc, SrcM1, "", CI), CI);
710710 CI->replaceAllUsesWith(Src);
711711 break;
712712 }
11541154 case Instruction::Shl:
11551155 case Instruction::LShr:
11561156 case Instruction::AShr:{
1157 Out << "BinaryOperator* " << iName << " = BinaryOperator::create(";
1157 Out << "BinaryOperator* " << iName << " = BinaryOperator::Create(";
11581158 switch (I->getOpcode()) {
11591159 case Instruction::Add: Out << "Instruction::Add"; break;
11601160 case Instruction::Sub: Out << "Instruction::Sub"; break;
871871 case ICmpInst::ICMP_ULE:
872872 case ICmpInst::ICMP_SLE:
873873 case ICmpInst::ICMP_EQ:
874 LV = BinaryOperator::createNot(LV, "notinit", CI);
874 LV = BinaryOperator::CreateNot(LV, "notinit", CI);
875875 break;
876876 case ICmpInst::ICMP_NE:
877877 case ICmpInst::ICMP_UGE:
11921192 if (!RunningOr)
11931193 RunningOr = Cond; // First seteq
11941194 else
1195 RunningOr = BinaryOperator::createOr(RunningOr, Cond, "tmp", MI);
1195 RunningOr = BinaryOperator::CreateOr(RunningOr, Cond, "tmp", MI);
11961196 }
11971197
11981198 // Split the basic block at the old malloc.
7171 GlobalValue::LinkOnceLinkage,
7272 "malloc_llvm_bounce", &M);
7373 BasicBlock* bb = BasicBlock::Create("entry",FN);
74 Instruction* c = CastInst::createIntegerCast(
74 Instruction* c = CastInst::CreateIntegerCast(
7575 FN->arg_begin(), Type::Int32Ty, false, "c", bb);
7676 Instruction* a = new MallocInst(Type::Int8Ty, c, "m", bb);
7777 ReturnInst::Create(a, bb);
163163 // source size.
164164 if (Source->getType() != Type::Int32Ty)
165165 Source =
166 CastInst::createIntegerCast(Source, Type::Int32Ty, false/*ZExt*/,
166 CastInst::CreateIntegerCast(Source, Type::Int32Ty, false/*ZExt*/,
167167 "MallocAmtCast", I);
168168
169169 MallocInst *MI = new MallocInst(Type::Int8Ty, Source, "", I);
6767 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
6868 false);
6969 InitCall->setOperand(2,
70 CastInst::create(opcode, AI, ArgVTy, "argv.cast", InitCall));
70 CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall));
7171 } else {
7272 InitCall->setOperand(2, AI);
7373 }
8282 if (!AI->use_empty()) {
8383 opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true);
8484 AI->replaceAllUsesWith(
85 CastInst::create(opcode, InitCall, AI->getType(), "", InsertPos));
85 CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos));
8686 }
8787 opcode = CastInst::getCastOpcode(AI, true, Type::Int32Ty, true);
8888 InitCall->setOperand(1,
89 CastInst::create(opcode, AI, Type::Int32Ty, "argc.cast", InitCall));
89 CastInst::Create(opcode, AI, Type::Int32Ty, "argc.cast", InitCall));
9090 } else {
9191 AI->replaceAllUsesWith(InitCall);
9292 InitCall->setOperand(1, AI);
112112
113113 // Load, increment and store the value back.
114114 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
115 Value *NewVal = BinaryOperator::create(Instruction::Add, OldVal,
115 Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal,
116116 ConstantInt::get(Type::Int32Ty, 1),
117117 "NewFuncCounter", InsertPos);
118118 new StoreInst(NewVal, ElementPtr, InsertPos);
214214 ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0),
215215 "countercc", t);
216216
217 Value* nv = BinaryOperator::createSub(l, ConstantInt::get(T, 1),
217 Value* nv = BinaryOperator::CreateSub(l, ConstantInt::get(T, 1),
218218 "counternew", t);
219219 new StoreInst(nv, Counter, t);
220220 t->setCondition(s);
289289 ICmpInst* s = new ICmpInst(ICmpInst::ICMP_EQ, l, ConstantInt::get(T, 0),
290290 "countercc", t);
291291
292 Value* nv = BinaryOperator::createSub(l, ConstantInt::get(T, 1),
292 Value* nv = BinaryOperator::CreateSub(l, ConstantInt::get(T, 1),
293293 "counternew", t);
294294 new StoreInst(nv, AI, t);
295295 t->setCondition(s);
318318
319319 CallInst* c = CallInst::Create(F, "rdcc", t);
320320 BinaryOperator* b =
321 BinaryOperator::createAnd(c, ConstantInt::get(Type::Int64Ty, rm),
321 BinaryOperator::CreateAnd(c, ConstantInt::get(Type::Int64Ty, rm),
322322 "mrdcc", t);
323323
324324 ICmpInst *s = new ICmpInst(ICmpInst::ICMP_EQ, b,
356356 // Load, increment and store the value back.
357357 Value *OldVal = new LoadInst(ElementPtr, "OldCounter", InsertPos);
358358 profcode.insert(OldVal);
359 Value *NewVal = BinaryOperator::createAdd(OldVal,
359 Value *NewVal = BinaryOperator::CreateAdd(OldVal,
360360 ConstantInt::get(Type::Int32Ty, 1),
361361 "NewCounter", InsertPos);
362362 profcode.insert(NewVal);
388388 while (isa(InsertPt)) ++InsertPt;
389389
390390 InsertedCast =
391 CastInst::create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "",
391 CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "",
392392 InsertPt);
393393 MadeChange = true;
394394 }
446446 while (isa(InsertPt)) ++InsertPt;
447447
448448 InsertedCmp =
449 CmpInst::create(CI->getOpcode(), CI->getPredicate(), CI->getOperand(0),
449 CmpInst::Create(CI->getOpcode(), CI->getPredicate(), CI->getOperand(0),
450450 CI->getOperand(1), "", InsertPt);
451451 MadeChange = true;
452452 }
879879 V = new SExtInst(V, IntPtrTy, "sunkaddr", InsertPt);
880880 }
881881 if (AddrMode.Scale != 1)
882 V = BinaryOperator::createMul(V, ConstantInt::get(IntPtrTy,
882 V = BinaryOperator::CreateMul(V, ConstantInt::get(IntPtrTy,
883883 AddrMode.Scale),
884884 "sunkaddr", InsertPt);
885885 Result = V;
891891 if (V->getType() != IntPtrTy)
892892 V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt);
893893 if (Result)
894 Result = BinaryOperator::createAdd(Result, V, "sunkaddr", InsertPt);
894 Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
895895 else
896896 Result = V;
897897 }
901901 Value *V = new PtrToIntInst(AddrMode.BaseGV, IntPtrTy, "sunkaddr",
902902 InsertPt);
903903 if (Result)
904 Result = BinaryOperator::createAdd(Result, V, "sunkaddr", InsertPt);
904 Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
905905 else
906906 Result = V;
907907 }
910910 if (AddrMode.BaseOffs) {
911911 Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
912912 if (Result)
913 Result = BinaryOperator::createAdd(Result, V, "sunkaddr", InsertPt);
913 Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
914914 else
915915 Result = V;
916916 }
803803 if (newOp1 != U->getOperand(0)) {
804804 Instruction* newVal = 0;
805805 if (CastInst* C = dyn_cast(U))
806 newVal = CastInst::create(C->getOpcode(),
806 newVal = CastInst::Create(C->getOpcode(),
807807 newOp1, C->getType(),
808808 C->getName()+".expr");
809809
846846 if (newOp1 != U->getOperand(0) || newOp2 != U->getOperand(1)) {
847847 Instruction* newVal = 0;
848848 if (BinaryOperator* BO = dyn_cast(U))
849 newVal = BinaryOperator::create(BO->getOpcode(),
849 newVal = BinaryOperator::Create(BO->getOpcode(),
850850 newOp1, newOp2,
851851 BO->getName()+".expr");
852852 else if (CmpInst* C = dyn_cast(U))
853 newVal = CmpInst::create(C->getOpcode(),
853 newVal = CmpInst::Create(C->getOpcode(),
854854 C->getPredicate(),
855855 newOp1, newOp2,
856856 C->getName()+".expr");
16641664
16651665 Value* newVal = 0;
16661666 if (BinaryOperator* BO = dyn_cast(U))
1667 newVal = BinaryOperator::create(BO->getOpcode(), s1, s2,
1667 newVal = BinaryOperator::Create(BO->getOpcode(), s1, s2,
16681668 BO->getName()+".gvnpre",
16691669 (*PI)->getTerminator());
16701670 else if (CmpInst* C = dyn_cast(U))
1671 newVal = CmpInst::create(C->getOpcode(), C->getPredicate(), s1, s2,
1671 newVal = CmpInst::Create(C->getOpcode(), C->getPredicate(), s1, s2,
16721672 C->getName()+".gvnpre",
16731673 (*PI)->getTerminator());
16741674 else if (ShuffleVectorInst* S = dyn_cast(U))
16841684 newVal = SelectInst::Create(s1, s2, s3, S->getName()+".gvnpre",
16851685 (*PI)->getTerminator());
16861686 else if (CastInst* C = dyn_cast(U))
1687 newVal = CastInst::create(C->getOpcode(), s1, C->getType(),
1687 newVal = CastInst::Create(C->getOpcode(), s1, C->getType(),
16881688 C->getName()+".gvnpre",
16891689 (*PI)->getTerminator());
16901690 else if (GetElementPtrInst* G = dyn_cast(U))
150150 NewPhi->addIncoming(Constant::getNullValue(NewPhi->getType()), Preheader);
151151
152152 // Create the new add instruction.
153 Value *NewAdd = BinaryOperator::createAdd(NewPhi, AddedVal,
153 Value *NewAdd = BinaryOperator::CreateAdd(NewPhi, AddedVal,
154154 GEPI->getName()+".rec", GEPI);
155155 NewPhi->addIncoming(NewAdd, PN->getIncomingBlock(BackedgeIdx));
156156
262262 if (Constant *CV = dyn_cast(V))
263263 return ConstantExpr::getCast(opc, CV, Ty);
264264
265 Instruction *C = CastInst::create(opc, V, Ty, V->getName(), &Pos);
265 Instruction *C = CastInst::Create(opc, V, Ty, V->getName(), &Pos);
266266 AddToWorkList(C);
267267 return C;
268268 }
512512
513513 // Fold (op (op V1, C1), (op V2, C2)) ==> (op (op V1, V2), (op C1,C2))
514514 Constant *Folded = ConstantExpr::get(I.getOpcode(), C1, C2);
515 Instruction *New = BinaryOperator::create(Opcode, Op->getOperand(0),
515 Instruction *New = BinaryOperator::Create(Opcode, Op->getOperand(0),
516516 Op1->getOperand(0),
517517 Op1->getName(), &I);
518518 AddToWorkList(New);
13921392 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
13931393 if ((DemandedMask & ~RHSKnownZero & ~LHSKnownZero) == 0) {
13941394 Instruction *Or =
1395 BinaryOperator::createOr(I->getOperand(0), I->getOperand(1),
1395 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
13961396 I->getName());
13971397 InsertNewInstBefore(Or, *I);
13981398 return UpdateValueUsesWith(I, Or);
14071407 if ((RHSKnownOne & LHSKnownOne) == RHSKnownOne) {
14081408 Constant *AndC = ConstantInt::get(~RHSKnownOne & DemandedMask);
14091409 Instruction *And =
1410 BinaryOperator::createAnd(I->getOperand(0), AndC, "tmp");
1410 BinaryOperator::CreateAnd(I->getOperand(0), AndC, "tmp");
14111411 InsertNewInstBefore(And, *I);
14121412 return UpdateValueUsesWith(I, And);
14131413 }
15661566 // Turn it into OR if input bits are zero.
15671567 if ((LHSKnownZero & RHS->getValue()) == RHS->getValue()) {
15681568 Instruction *Or =
1569 BinaryOperator::createOr(I->getOperand(0), I->getOperand(1),
1569 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1),
15701570 I->getName());
15711571 InsertNewInstBefore(Or, *I);
15721572 return UpdateValueUsesWith(I, Or);
16741674 // the shift amount is >= the size of the datatype, which is undefined.
16751675 if (DemandedMask == 1) {
16761676 // Perform the logical shift right.
1677 Value *NewVal = BinaryOperator::createLShr(
1677 Value *NewVal = BinaryOperator::CreateLShr(
16781678 I->getOperand(0), I->getOperand(1), I->getName());
16791679 InsertNewInstBefore(cast(NewVal), *I);
16801680 return UpdateValueUsesWith(I, NewVal);
17151715 if (RHSKnownZero[BitWidth-ShiftAmt-1] ||
17161716 (HighBits & ~DemandedMask) == HighBits) {
17171717 // Perform the logical shift right.
1718 Value *NewVal = BinaryOperator::createLShr(
1718 Value *NewVal = BinaryOperator::CreateLShr(
17191719 I->getOperand(0), SA, I->getName());
17201720 InsertNewInstBefore(cast(NewVal), *I);
17211721 return UpdateValueUsesWith(I, NewVal);
20252025 default: assert(0 && "Case stmts out of sync!");
20262026 case Intrinsic::x86_sse_sub_ss:
20272027 case Intrinsic::x86_sse2_sub_sd:
2028 TmpV = InsertNewInstBefore(BinaryOperator::createSub(LHS, RHS,
2028 TmpV = InsertNewInstBefore(BinaryOperator::CreateSub(LHS, RHS,
20292029 II->getName()), *II);
20302030 break;
20312031 case Intrinsic::x86_sse_mul_ss:
20322032 case Intrinsic::x86_sse2_mul_sd:
2033 TmpV = InsertNewInstBefore(BinaryOperator::createMul(LHS, RHS,
2033 TmpV = InsertNewInstBefore(BinaryOperator::CreateMul(LHS, RHS,
20342034 II->getName()), *II);
20352035 break;
20362036 }
21592159 AddRHS(Value *rhs) : RHS(rhs) {}
21602160 bool shouldApply(Value *LHS) const { return LHS == RHS; }
21612161 Instruction *apply(BinaryOperator &Add) const {
2162 return BinaryOperator::createShl(Add.getOperand(0),
2162 return BinaryOperator::CreateShl(Add.getOperand(0),
21632163 ConstantInt::get(Add.getType(), 1));
21642164 }
21652165 };
21752175 ConstantExpr::getAnd(C1, C2)->isNullValue();
21762176 }
21772177 Instruction *apply(BinaryOperator &Add) const {
2178 return BinaryOperator::createOr(Add.getOperand(0), Add.getOperand(1));
2178 return BinaryOperator::CreateOr(Add.getOperand(0), Add.getOperand(1));
21792179 }
21802180 };
21812181
21872187 if (Constant *SOC = dyn_cast(SO))
21882188 return ConstantExpr::getCast(CI->getOpcode(), SOC, I.getType());
21892189
2190 return IC->InsertNewInstBefore(CastInst::create(
2190 return IC->InsertNewInstBefore(CastInst::Create(
21912191 CI->getOpcode(), SO, I.getType(), SO->getName() + ".cast"), I);
21922192 }
21932193
22062206 std::swap(Op0, Op1);
22072207 Instruction *New;
22082208 if (BinaryOperator *BO = dyn_cast(&I))
2209 New = BinaryOperator::create(BO->getOpcode(), Op0, Op1,SO->getName()+".op");
2209 New = BinaryOperator::Create(BO->getOpcode(), Op0, Op1,SO->getName()+".op");
22102210 else if (CmpInst *CI = dyn_cast(&I))
2211 New = CmpInst::create(CI->getOpcode(), CI->getPredicate(), Op0, Op1,
2211 New = CmpInst::Create(CI->getOpcode(), CI->getPredicate(), Op0, Op1,
22122212 SO->getName()+".cmp");
22132213 else {
22142214 assert(0 && "Unknown binary instruction type!");
22942294 } else {
22952295 assert(PN->getIncomingBlock(i) == NonConstBB);
22962296 if (BinaryOperator *BO = dyn_cast(&I))
2297 InV = BinaryOperator::create(BO->getOpcode(),
2297 InV = BinaryOperator::Create(BO->getOpcode(),
22982298 PN->getIncomingValue(i), C, "phitmp",
22992299 NonConstBB->getTerminator());
23002300 else if (CmpInst *CI = dyn_cast(&I))
2301 InV = CmpInst::create(CI->getOpcode(),
2301 InV = CmpInst::Create(CI->getOpcode(),
23022302 CI->getPredicate(),
23032303 PN->getIncomingValue(i), C, "phitmp",
23042304 NonConstBB->getTerminator());
23182318 InV = ConstantExpr::getCast(CI->getOpcode(), InC, RetTy);
23192319 } else {
23202320 assert(PN->getIncomingBlock(i) == NonConstBB);
2321 InV = CastInst::create(CI->getOpcode(), PN->getIncomingValue(i),
2321 InV = CastInst::Create(CI->getOpcode(), PN->getIncomingValue(i),
23222322 I.getType(), "phitmp",
23232323 NonConstBB->getTerminator());
23242324 AddToWorkList(cast(InV));
23952395 const APInt& Val = CI->getValue();
23962396 uint32_t BitWidth = Val.getBitWidth();
23972397 if (Val == APInt::getSignBit(BitWidth))
2398 return BinaryOperator::createXor(LHS, RHS);
2398 return BinaryOperator::CreateXor(LHS, RHS);
23992399
24002400 // See if SimplifyDemandedBits can simplify this. This handles stuff like
24012401 // (X & 254)+1 -> (X&254)|1
24792479 if (Value *LHSV = dyn_castNegVal(LHS)) {
24802480 if (LHS->getType()->isIntOrIntVector()) {
24812481 if (Value *RHSV = dyn_castNegVal(RHS)) {
2482 Instruction *NewAdd = BinaryOperator::createAdd(LHSV, RHSV, "sum");
2482 Instruction *NewAdd = BinaryOperator::CreateAdd(LHSV, RHSV, "sum");
24832483 InsertNewInstBefore(NewAdd, I);
2484 return BinaryOperator::createNeg(NewAdd);
2485 }
2486 }
2487
2488 return BinaryOperator::createSub(RHS, LHSV);
2484 return BinaryOperator::CreateNeg(NewAdd);
2485 }
2486 }
2487
2488 return BinaryOperator::CreateSub(RHS, LHSV);
24892489 }
24902490
24912491 // A + -B --> A - B
24922492 if (!isa(RHS))
24932493 if (Value *V = dyn_castNegVal(RHS))
2494 return BinaryOperator::createSub(LHS, V);
2494 return BinaryOperator::CreateSub(LHS, V);
24952495
24962496
24972497 ConstantInt *C2;
24982498 if (Value *X = dyn_castFoldableMul(LHS, C2)) {
24992499 if (X == RHS) // X*C + X --> X * (C+1)
2500 return BinaryOperator::createMul(RHS, AddOne(C2));
2500 return BinaryOperator::CreateMul(RHS, AddOne(C2));
25012501
25022502 // X*C1 + X*C2 --> X * (C1+C2)
25032503 ConstantInt *C1;
25042504 if (X == dyn_castFoldableMul(RHS, C1))
2505 return BinaryOperator::createMul(X, Add(C1, C2));
2505 return BinaryOperator::CreateMul(X, Add(C1, C2));
25062506 }
25072507
25082508 // X + X*C --> X * (C+1)
25092509 if (dyn_castFoldableMul(RHS, C2) == LHS)
2510 return BinaryOperator::createMul(LHS, AddOne(C2));
2510 return BinaryOperator::CreateMul(LHS, AddOne(C2));
25112511
25122512 // X + ~X --> -1 since ~X = -X-1
25132513 if (dyn_castNotVal(LHS) == RHS || dyn_castNotVal(RHS) == LHS)
25362536 }
25372537
25382538 if (W == Y) {
2539 Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, Z,
2539 Value *NewAdd = InsertNewInstBefore(BinaryOperator::CreateAdd(X, Z,
25402540 LHS->getName()), I);
2541 return BinaryOperator::createMul(W, NewAdd);
2541 return BinaryOperator::CreateMul(W, NewAdd);
25422542 }
25432543 }
25442544 }
25462546 if (ConstantInt *CRHS = dyn_cast(RHS)) {
25472547 Value *X = 0;
25482548 if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X
2549 return BinaryOperator::createSub(SubOne(CRHS), X);
2549 return BinaryOperator::CreateSub(SubOne(CRHS), X);
25502550
25512551 // (X & FF00) + xx00 -> (X+xx00) & FF00
25522552 if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) {
25642564
25652565 if (AddRHSHighBits == AddRHSHighBitsAnd) {
25662566 // Okay, the xform is safe. Insert the new add pronto.
2567 Value *NewAdd = InsertNewInstBefore(BinaryOperator::createAdd(X, CRHS,
2567 Value *NewAdd = InsertNewInstBefore(BinaryOperator::CreateAdd(X, CRHS,
25682568 LHS->getName()), I);
2569 return BinaryOperator::createAnd(NewAdd, C2);
2569 return BinaryOperator::CreateAnd(NewAdd, C2);
25702570 }
25712571 }
25722572 }
26462646
26472647 // If this is a 'B = x-(-A)', change to B = x+A...
26482648 if (Value *V = dyn_castNegVal(Op1))
2649 return BinaryOperator::createAdd(Op0, V);
2649 return BinaryOperator::CreateAdd(Op0, V);
26502650
26512651 if (isa(Op0))
26522652 return ReplaceInstUsesWith(I, Op0); // undef - X -> undef
26562656 if (ConstantInt *C = dyn_cast(Op0)) {
26572657 // Replace (-1 - A) with (~A)...
26582658 if (C->isAllOnesValue())
2659 return BinaryOperator::createNot(Op1);
2659 return BinaryOperator::CreateNot(Op1);
26602660
26612661 // C - ~X == X + (1+C)
26622662 Value *X = 0;
26632663 if (match(Op1, m_Not(m_Value(X))))
2664 return BinaryOperator::createAdd(X, AddOne(C));
2664 return BinaryOperator::CreateAdd(X, AddOne(C));
26652665
26662666 // -(X >>u 31) -> (X >>s 31)
26672667 // -(X >>s 31) -> (X >>u 31)
26732673 if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) ==
26742674 SI->getType()->getPrimitiveSizeInBits()-1) {
26752675 // Ok, the transformation is safe. Insert AShr.
2676 return BinaryOperator::create(Instruction::AShr,
2676 return BinaryOperator::Create(Instruction::AShr,
26772677 SI->getOperand(0), CU, SI->getName());
26782678 }
26792679 }
26842684 if (CU->getLimitedValue(SI->getType()->getPrimitiveSizeInBits()) ==
26852685 SI->getType()->getPrimitiveSizeInBits()-1) {
26862686 // Ok, the transformation is safe. Insert LShr.
2687 return BinaryOperator::createLShr(
2687 return BinaryOperator::CreateLShr(
26882688 SI->getOperand(0), CU, SI->getName());
26892689 }
26902690 }
27062706 if (Op1I->getOpcode() == Instruction::Add &&
27072707 !Op0->getType()->isFPOrFPVector()) {
27082708 if (Op1I->getOperand(0) == Op0) // X-(X+Y) == -Y
2709 return BinaryOperator::createNeg(Op1I->getOperand(1), I.getName());
2709 return BinaryOperator::CreateNeg(Op1I->getOperand(1), I.getName());
27102710 else if (Op1I->getOperand(1) == Op0) // X-(Y+X) == -Y
2711 return BinaryOperator::createNeg(Op1I->getOperand(0), I.getName());
2711 return BinaryOperator::CreateNeg(Op1I->getOperand(0), I.getName());
27122712 else if (ConstantInt *CI1 = dyn_cast(I.getOperand(0))) {
27132713 if (ConstantInt *CI2 = dyn_cast(Op1I->getOperand(1)))
27142714 // C1-(X+C2) --> (C1-C2)-X
2715 return BinaryOperator::createSub(Subtract(CI1, CI2),
2715 return BinaryOperator::CreateSub(Subtract(CI1, CI2),
27162716 Op1I->getOperand(0));
27172717 }
27182718 }
27292729 Op1I->setOperand(1, IIOp0);
27302730
27312731 // Create the new top level add instruction...
2732 return BinaryOperator::createAdd(Op0, Op1);
2732 return BinaryOperator::CreateAdd(Op0, Op1);
27332733 }
27342734
27352735 // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)...
27392739 Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0);
27402740
27412741 Value *NewNot =
2742 InsertNewInstBefore(BinaryOperator::createNot(OtherOp, "B.not"), I);
2743 return BinaryOperator::createAnd(Op0, NewNot);
2742 InsertNewInstBefore(BinaryOperator::CreateNot(OtherOp, "B.not"), I);
2743 return BinaryOperator::CreateAnd(Op0, NewNot);
27442744 }
27452745
27462746 // 0 - (X sdiv C) -> (X sdiv -C)
27482748 if (ConstantInt *CSI = dyn_cast(Op0))
27492749 if (CSI->isZero())
27502750 if (Constant *DivRHS = dyn_cast(Op1I->getOperand(1)))
2751 return BinaryOperator::createSDiv(Op1I->getOperand(0),
2751 return BinaryOperator::CreateSDiv(Op1I->getOperand(0),
27522752 ConstantExpr::getNeg(DivRHS));
27532753
27542754 // X - X*C --> X * (1-C)
27552755 ConstantInt *C2 = 0;
27562756 if (dyn_castFoldableMul(Op1I, C2) == Op0) {
27572757 Constant *CP1 = Subtract(ConstantInt::get(I.getType(), 1), C2);
2758 return BinaryOperator::createMul(Op0, CP1);
2758 return BinaryOperator::CreateMul(Op0, CP1);
27592759 }
27602760
27612761 // X - ((X / Y) * Y) --> X % Y
27642764 if (Op0 == I->getOperand(0) &&
27652765 Op1I->getOperand(1) == I->getOperand(1)) {
27662766 if (I->getOpcode() == Instruction::SDiv)
2767 return BinaryOperator::createSRem(Op0, Op1I->getOperand(1));
2767 return BinaryOperator::CreateSRem(Op0, Op1I->getOperand(1));
27682768 if (I->getOpcode() == Instruction::UDiv)
2769 return BinaryOperator::createURem(Op0, Op1I->getOperand(1));
2769 return BinaryOperator::CreateURem(Op0, Op1I->getOperand(1));
27702770 }
27712771 }
27722772 }
27802780 return ReplaceInstUsesWith(I, Op0I->getOperand(0));
27812781 } else if (Op0I->getOpcode() == Instruction::Sub) {
27822782 if (Op0I->getOperand(0) == Op1) // (X-Y)-X == -Y
2783 return BinaryOperator::createNeg(Op0I->getOperand(1), I.getName());
2783 return BinaryOperator::CreateNeg(Op0I->getOperand(1), I.getName());
27842784 }
27852785 }
27862786
27872787 ConstantInt *C1;
27882788 if (Value *X = dyn_castFoldableMul(Op0, C1)) {
27892789 if (X == Op1) // X*C - X --> X * (C-1)
2790 return BinaryOperator::createMul(Op1, SubOne(C1));
2790 return BinaryOperator::CreateMul(Op1, SubOne(C1));
27912791
27922792 ConstantInt *C2; // X*C1 - X*C2 -> X * (C1-C2)
27932793 if (X == dyn_castFoldableMul(Op1, C2))
2794 return BinaryOperator::createMul(X, Subtract(C1, C2));
2794 return BinaryOperator::CreateMul(X, Subtract(C1, C2));
27952795 }
27962796 return 0;
27972797 }
28422842 if (BinaryOperator *SI = dyn_cast(Op0))
28432843 if (SI->getOpcode() == Instruction::Shl)
28442844 if (Constant *ShOp = dyn_cast(SI->getOperand(1)))
2845 return BinaryOperator::createMul(SI->getOperand(0),
2845 return BinaryOperator::CreateMul(SI->getOperand(0),
28462846 ConstantExpr::getShl(CI, ShOp));
28472847
28482848 if (CI->isZero())
28502850 if (CI->equalsInt(1)) // X * 1 == X
28512851 return ReplaceInstUsesWith(I, Op0);
28522852 if (CI->isAllOnesValue()) // X * -1 == 0 - X
2853 return BinaryOperator::createNeg(Op0, I.getName());
2853 return BinaryOperator::CreateNeg(Op0, I.getName());
28542854
28552855 const APInt& Val = cast(CI)->getValue();
28562856 if (Val.isPowerOf2()) { // Replace X*(2^C) with X << C
2857 return BinaryOperator::createShl(Op0,
2857 return BinaryOperator::CreateShl(Op0,
28582858 ConstantInt::get(Op0->getType(), Val.logBase2()));
28592859 }
28602860 } else if (ConstantFP *Op1F = dyn_cast(Op1)) {
28732873 if (Op0I->getOpcode() == Instruction::Add && Op0I->hasOneUse() &&
28742874 isa(Op0I->getOperand(1))) {
28752875 // Canonicalize (X+C1)*C2 -> X*C2+C1*C2.
2876 Instruction *Add = BinaryOperator::createMul(Op0I->getOperand(0),
2876 Instruction *Add = BinaryOperator::CreateMul(Op0I->getOperand(0),
28772877 Op1, "tmp");
28782878 InsertNewInstBefore(Add, I);
28792879 Value *C1C2 = ConstantExpr::getMul(Op1,
28802880 cast(Op0I->getOperand(1)));
2881 return BinaryOperator::createAdd(Add, C1C2);
2881 return BinaryOperator::CreateAdd(Add, C1C2);
28822882
28832883 }
28842884
28942894
28952895 if (Value *Op0v = dyn_castNegVal(Op0)) // -X * -Y = X*Y
28962896 if (Value *Op1v = dyn_castNegVal(I.getOperand(1)))
2897 return BinaryOperator::createMul(Op0v, Op1v);
2897 return BinaryOperator::CreateMul(Op0v, Op1v);
28982898
28992899 // If one of the operands of the multiply is a cast from a boolean value, then
29002900 // we know the bool is either zero or one, so this is a 'masking' multiply.
29242924 SCOpTy->getPrimitiveSizeInBits()-1);
29252925 Value *V =
29262926 InsertNewInstBefore(
2927 BinaryOperator::create(Instruction::AShr, SCIOp0, Amt,
2927 BinaryOperator::Create(Instruction::AShr, SCIOp0, Amt,
29282928 BoolCast->getOperand(0)->getName()+
29292929 ".mask"), I);
29302930
29402940 }
29412941
29422942 Value *OtherOp = Op0 == BoolCast ? I.getOperand(1) : Op0;
2943 return BinaryOperator::createAnd(V, OtherOp);
2943 return BinaryOperator::CreateAnd(V, OtherOp);
29442944 }
29452945 }
29462946 }
30303030 if (MultiplyOverflows(RHS, LHSRHS, I.getOpcode()==Instruction::SDiv))
30313031 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
30323032 else
3033 return BinaryOperator::create(I.getOpcode(), LHS->getOperand(0),
3033 return BinaryOperator::Create(I.getOpcode(), LHS->getOperand(0),
30343034 Multiply(RHS, LHSRHS));
30353035 }
30363036
30643064 // if so, convert to a right shift.
30653065 if (ConstantInt *C = dyn_cast(Op1)) {
30663066 if (C->getValue().isPowerOf2()) // 0 not included in isPowerOf2
3067 return BinaryOperator::createLShr(Op0,
3067 return BinaryOperator::CreateLShr(Op0,
30683068 ConstantInt::get(Op0->getType(), C->getValue().logBase2()));
30693069 }
30703070
30783078 const Type *NTy = N->getType();
30793079 if (uint32_t C2 = C1.logBase2()) {
30803080 Constant *C2V = ConstantInt::get(NTy, C2);
3081 N = InsertNewInstBefore(BinaryOperator::createAdd(N, C2V, "tmp"), I);
3081 N = InsertNewInstBefore(BinaryOperator::CreateAdd(N, C2V, "tmp"), I);
30823082 }
3083 return BinaryOperator::createLShr(Op0, N);
3083 return BinaryOperator::CreateLShr(Op0, N);
30843084 }
30853085 }
30863086 }
30963096 uint32_t TSA = TVA.logBase2(), FSA = FVA.logBase2();
30973097 // Construct the "on true" case of the select
30983098 Constant *TC = ConstantInt::get(Op0->getType(), TSA);
3099 Instruction *TSI = BinaryOperator::createLShr(
3099 Instruction *TSI = BinaryOperator::CreateLShr(
31003100 Op0, TC, SI->getName()+".t");
31013101 TSI = InsertNewInstBefore(TSI, I);
31023102
31033103 // Construct the "on false" case of the select
31043104 Constant *FC = ConstantInt::get(Op0->getType(), FSA);
3105 Instruction *FSI = BinaryOperator::createLShr(
3105 Instruction *FSI = BinaryOperator::CreateLShr(
31063106 Op0, FC, SI->getName()+".f");
31073107 FSI = InsertNewInstBefore(FSI, I);
31083108
31233123 if (ConstantInt *RHS = dyn_cast(Op1)) {
31243124 // sdiv X, -1 == -X
31253125 if (RHS->isAllOnesValue())
3126 return BinaryOperator::createNeg(Op0);
3126 return BinaryOperator::CreateNeg(Op0);
31273127
31283128 // -X/C -> X/-C
31293129 if (Value *LHSNeg = dyn_castNegVal(Op0))
3130 return BinaryOperator::createSDiv(LHSNeg, ConstantExpr::getNeg(RHS));
3130 return BinaryOperator::CreateSDiv(LHSNeg, ConstantExpr::getNeg(RHS));
31313131 }
31323132
31333133 // If the sign bits of both operands are zero (i.e. we can prove they are
31363136 APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
31373137 if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
31383138 // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
3139 return BinaryOperator::createUDiv(Op0, Op1, I.getName());
3139 return BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
31403140 }
31413141 }
31423142
32533253 // if so, convert to a bitwise and.
32543254 if (ConstantInt *C = dyn_cast(RHS))
32553255 if (C->getValue().isPowerOf2())
3256 return BinaryOperator::createAnd(Op0, SubOne(C));
3256 return BinaryOperator::CreateAnd(Op0, SubOne(C));
32573257 }
32583258
32593259 if (Instruction *RHSI = dyn_cast(I.getOperand(1))) {
32623262 isa(RHSI->getOperand(0))) {
32633263 if (cast(RHSI->getOperand(0))->getValue().isPowerOf2()) {
32643264 Constant *N1 = ConstantInt::getAllOnesValue(I.getType());
3265 Value *Add = InsertNewInstBefore(BinaryOperator::createAdd(RHSI, N1,
3265 Value *Add = InsertNewInstBefore(BinaryOperator::CreateAdd(RHSI, N1,
32663266 "tmp"), I);
3267 return BinaryOperator::createAnd(Op0, Add);
3267 return BinaryOperator::CreateAnd(Op0, Add);
32683268 }
32693269 }
32703270 }
32783278 if ((STO->getValue().isPowerOf2()) &&
32793279 (SFO->getValue().isPowerOf2())) {
32803280 Value *TrueAnd = InsertNewInstBefore(
3281 BinaryOperator::createAnd(Op0, SubOne(STO), SI->getName()+".t"), I);
3281 BinaryOperator::CreateAnd(Op0, SubOne(STO), SI->getName()+".t"), I);
32823282 Value *FalseAnd = InsertNewInstBefore(
3283 BinaryOperator::createAnd(Op0, SubOne(SFO), SI->getName()+".f"), I);
3283 BinaryOperator::CreateAnd(Op0, SubOne(SFO), SI->getName()+".f"), I);
32843284 return SelectInst::Create(SI->getOperand(0), TrueAnd, FalseAnd);
32853285 }
32863286 }
33113311 APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
33123312 if (MaskedValueIsZero(Op1, Mask) && MaskedValueIsZero(Op0, Mask)) {
33133313 // X srem Y -> X urem Y, iff X and Y don't have sign bit set
3314 return BinaryOperator::createURem(Op0, Op1, I.getName());
3314 return BinaryOperator::CreateURem(Op0, Op1, I.getName());
33153315 }
33163316 }
33173317
34993499 case Instruction::Xor:
35003500 if (Op->hasOneUse()) {
35013501 // (X ^ C1) & C2 --> (X & C2) ^ (C1&C2)
3502 Instruction *And = BinaryOperator::createAnd(X, AndRHS);
3502 Instruction *And = BinaryOperator::CreateAnd(X, AndRHS);
35033503 InsertNewInstBefore(And, TheAnd);
35043504 And->takeName(Op);
3505 return BinaryOperator::createXor(And, Together);
3505 return BinaryOperator::CreateXor(And, Together);
35063506 }
35073507 break;
35083508 case Instruction::Or:
35113511
35123512 if (Op->hasOneUse() && Together != OpRHS) {
35133513 // (X | C1) & C2 --> (X | (C1&C2)) & C2
3514 Instruction *Or = BinaryOperator::createOr(X, Together);
3514 Instruction *Or = BinaryOperator::CreateOr(X, Together);
35153515 InsertNewInstBefore(Or, TheAnd);
35163516 Or->takeName(Op);
3517 return BinaryOperator::createAnd(Or, AndRHS);
3517 return BinaryOperator::CreateAnd(Or, AndRHS);
35183518 }
35193519 break;
35203520 case Instruction::Add:
35423542 return &TheAnd;
35433543 } else {
35443544 // Pull the XOR out of the AND.
3545 Instruction *NewAnd = BinaryOperator::createAnd(X, AndRHS);
3545 Instruction *NewAnd = BinaryOperator::CreateAnd(X, AndRHS);
35463546 InsertNewInstBefore(NewAnd, TheAnd);
35473547 NewAnd->takeName(Op);
3548 return BinaryOperator::createXor(NewAnd, AndRHS);
3548 return BinaryOperator::CreateXor(NewAnd, AndRHS);
35493549 }
35503550 }
35513551 }
36043604 // Make the argument unsigned.
36053605 Value *ShVal = Op->getOperand(0);
36063606 ShVal = InsertNewInstBefore(
3607 BinaryOperator::createLShr(ShVal, OpRHS,
3607 BinaryOperator::CreateLShr(ShVal, OpRHS,
36083608 Op->getName()), TheAnd);
3609 return BinaryOperator::createAnd(ShVal, AndRHS, TheAnd.getName());
3609 return BinaryOperator::CreateAnd(ShVal, AndRHS, TheAnd.getName());
36103610 }
36113611 }
36123612 break;
36403640
36413641 // Emit V-Lo
36423642 Constant *NegLo = ConstantExpr::getNeg(Lo);
3643 Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off");
3643 Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off");
36443644 InsertNewInstBefore(Add, IB);
36453645 Constant *UpperBound = ConstantExpr::getAdd(NegLo, Hi);
36463646 return new ICmpInst(ICmpInst::ICMP_ULT, Add, UpperBound);
36603660 // Emit V-Lo >u Hi-1-Lo
36613661 // Note that Hi has already had one subtracted from it, above.
36623662 ConstantInt *NegLo = cast(ConstantExpr::getNeg(Lo));
3663 Instruction *Add = BinaryOperator::createAdd(V, NegLo, V->getName()+".off");
3663 Instruction *Add = BinaryOperator::CreateAdd(V, NegLo, V->getName()+".off");
36643664 InsertNewInstBefore(Add, IB);
36653665 Constant *LowerBound = ConstantExpr::getAdd(NegLo, Hi);
36663666 return new ICmpInst(ICmpInst::ICMP_UGT, Add, LowerBound);
37353735
37363736 Instruction *New;
37373737 if (isSub)
3738 New = BinaryOperator::createSub(LHSI->getOperand(0), RHS, "fold");
3738 New = BinaryOperator::CreateSub(LHSI->getOperand(0), RHS, "fold");
37393739 else
3740 New = BinaryOperator::createAdd(LHSI->getOperand(0), RHS, "fold");
3740 New = BinaryOperator::CreateAdd(LHSI->getOperand(0), RHS, "fold");
37413741 return InsertNewInstBefore(New, I);
37423742 }
37433743
37853785 if (Op0I->hasOneUse()) {
37863786 if (MaskedValueIsZero(Op0LHS, NotAndRHS)) {
37873787 // Not masking anything out for the LHS, move to RHS.
3788 Instruction *NewRHS = BinaryOperator::createAnd(Op0RHS, AndRHS,
3788 Instruction *NewRHS = BinaryOperator::CreateAnd(Op0RHS, AndRHS,
37893789 Op0RHS->getName()+".masked");
37903790 InsertNewInstBefore(NewRHS, I);
3791 return BinaryOperator::create(
3791 return BinaryOperator::Create(
37923792 cast(Op0I)->getOpcode(), Op0LHS, NewRHS);
37933793 }
37943794 if (!isa(Op0RHS) &&
37953795 MaskedValueIsZero(Op0RHS, NotAndRHS)) {
37963796 // Not masking anything out for the RHS, move to LHS.
3797 Instruction *NewLHS = BinaryOperator::createAnd(Op0LHS, AndRHS,
3797 Instruction *NewLHS = BinaryOperator::CreateAnd(Op0LHS, AndRHS,
37983798 Op0LHS->getName()+".masked");
37993799 InsertNewInstBefore(NewLHS, I);
3800 return BinaryOperator::create(
3800 return BinaryOperator::Create(
38013801 cast(Op0I)->getOpcode(), NewLHS, Op0RHS);
38023802 }
38033803 }
38083808 // ((A | N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
38093809 // ((A ^ N) + B) & AndRHS -> (A + B) & AndRHS iff N&AndRHS == 0
38103810 if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, false, I))
3811 return BinaryOperator::createAnd(V, AndRHS);
3811 return BinaryOperator::CreateAnd(V, AndRHS);
38123812 if (Value *V = FoldLogicalPlusAnd(Op0RHS, Op0LHS, AndRHS, false, I))
3813 return BinaryOperator::createAnd(V, AndRHS); // Add commutes
3813 return BinaryOperator::CreateAnd(V, AndRHS); // Add commutes
38143814 break;
38153815
38163816 case Instruction::Sub:
38183818 // ((A | N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
38193819 // ((A ^ N) - B) & AndRHS -> (A - B) & AndRHS iff N&AndRHS == 0
38203820 if (Value *V = FoldLogicalPlusAnd(Op0LHS, Op0RHS, AndRHS, true, I))
3821 return BinaryOperator::createAnd(V, AndRHS);
3821 return BinaryOperator::CreateAnd(V, AndRHS);
38223822 break;
38233823 }
38243824
38383838 // into : and (cast X to T), trunc_or_bitcast(C1)&C2
38393839 // This will fold the two constants together, which may allow
38403840 // other simplifications.
3841 Instruction *NewCast = CastInst::createTruncOrBitCast(
3841 Instruction *NewCast = CastInst::CreateTruncOrBitCast(
38423842 CastOp->getOperand(0), I.getType(),
38433843 CastOp->getName()+".shrunk");
38443844 NewCast = InsertNewInstBefore(NewCast, I);
38453845 // trunc_or_bitcast(C1)&C2
38463846 Constant *C3 = ConstantExpr::getTruncOrBitCast(AndCI,I.getType());
38473847 C3 = ConstantExpr::getAnd(C3, AndRHS);
3848 return BinaryOperator::createAnd(NewCast, C3);
3848 return BinaryOperator::CreateAnd(NewCast, C3);
38493849 } else if (CastOp->getOpcode() == Instruction::Or) {
38503850 // Change: and (cast (or X, C1) to T), C2
38513851 // into : trunc(C1)&C2 iff trunc(C1)&C2 == C2
38743874
38753875 // (~A & ~B) == (~(A | B)) - De Morgan's Law
38763876 if (Op0NotVal && Op1NotVal && isOnlyUse(Op0) && isOnlyUse(Op1)) {
3877 Instruction *Or = BinaryOperator::createOr(Op0NotVal, Op1NotVal,
3877 Instruction *Or = BinaryOperator::CreateOr(Op0NotVal, Op1NotVal,
38783878 I.getName()+".demorgan");
38793879 InsertNewInstBefore(Or, I);
3880 return BinaryOperator::createNot(Or);
3880 return BinaryOperator::CreateNot(Or);
38813881 }
38823882
38833883 {
38893889 // (A|B) & ~(A&B) -> A^B
38903890 if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) {
38913891 if ((A == C && B == D) || (A == D && B == C))
3892 return BinaryOperator::createXor(A, B);
3892 return BinaryOperator::CreateXor(A, B);
38933893 }
38943894 }
38953895
39003900 // ~(A&B) & (A|B) -> A^B
39013901 if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) {
39023902 if ((A == C && B == D) || (A == D && B == C))
3903 return BinaryOperator::createXor(A, B);
3903 return BinaryOperator::CreateXor(A, B);
39043904 }
39053905 }
39063906
39223922 std::swap(A, B);
39233923 }
39243924 if (A == Op0) { // A&(A^B) -> A & ~B
3925 Instruction *NotB = BinaryOperator::createNot(B, "tmp");
3925 Instruction *NotB = BinaryOperator::CreateNot(B, "tmp");
39263926 InsertNewInstBefore(NotB, I);
3927 return BinaryOperator::createAnd(A, NotB);
3927 return BinaryOperator::CreateAnd(A, NotB);
39283928 }
39293929 }
39303930 }
40074007 case ICmpInst::ICMP_NE:
40084008 if (LHSCst == SubOne(RHSCst)){// (X != 13 & X != 14) -> X-13 >u 1
40094009 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
4010 Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST,
4010 Instruction *Add = BinaryOperator::CreateAdd(LHSVal, AddCST,
40114011 LHSVal->getName()+".off");
40124012 InsertNewInstBefore(Add, I);
40134013 return new ICmpInst(ICmpInst::ICMP_UGT, Add,
41004100 I.getType(), TD) &&
41014101 ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0),
41024102 I.getType(), TD)) {
4103 Instruction *NewOp = BinaryOperator::createAnd(Op0C->getOperand(0),
4103 Instruction *NewOp = BinaryOperator::CreateAnd(Op0C->getOperand(0),
41044104 Op1C->getOperand(0),
41054105 I.getName());
41064106 InsertNewInstBefore(NewOp, I);
4107 return CastInst::create(Op0C->getOpcode(), NewOp, I.getType());
4107 return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
41084108 }
41094109 }
41104110
41154115 SI0->getOperand(1) == SI1->getOperand(1) &&
41164116 (SI0->hasOneUse() || SI1->hasOneUse())) {
41174117 Instruction *NewOp =
4118 InsertNewInstBefore(BinaryOperator::createAnd(SI0->getOperand(0),
4118 InsertNewInstBefore(BinaryOperator::CreateAnd(SI0->getOperand(0),
41194119 SI1->getOperand(0),
41204120 SI0->getName()), I);
4121 return BinaryOperator::create(SI1->getOpcode(), NewOp,
4121 return BinaryOperator::Create(SI1->getOpcode(), NewOp,
41224122 SI1->getOperand(1));
41234123 }
41244124 }
42904290 ConstantInt *C1 = 0; Value *X = 0;
42914291 // (X & C1) | C2 --> (X | C2) & (C1|C2)
42924292 if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) {
4293 Instruction *Or = BinaryOperator::createOr(X, RHS);
4293 Instruction *Or = BinaryOperator::CreateOr(X, RHS);
42944294 InsertNewInstBefore(Or, I);
42954295 Or->takeName(Op0);
4296 return BinaryOperator::createAnd(Or,
4296 return BinaryOperator::CreateAnd(Or,
42974297 ConstantInt::get(RHS->getValue() | C1->getValue()));
42984298 }
42994299
43004300 // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
43014301 if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) {
4302 Instruction *Or = BinaryOperator::createOr(X, RHS);
4302 Instruction *Or = BinaryOperator::CreateOr(X, RHS);
43034303 InsertNewInstBefore(Or, I);
43044304 Or->takeName(Op0);
4305 return BinaryOperator::createXor(Or,
4305 return BinaryOperator::CreateXor(Or,
43064306 ConstantInt::get(C1->getValue() & ~RHS->getValue()));
43074307 }
43084308
43384338 // (X^C)|Y -> (X|Y)^C iff Y&C == 0
43394339 if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
43404340 MaskedValueIsZero(Op1, C1->getValue())) {
4341 Instruction *NOr = BinaryOperator::createOr(A, Op1);
4341 Instruction *NOr = BinaryOperator::CreateOr(A, Op1);
43424342 InsertNewInstBefore(NOr, I);
43434343 NOr->takeName(Op0);
4344 return BinaryOperator::createXor(NOr, C1);
4344 return BinaryOperator::CreateXor(NOr, C1);
43454345 }
43464346
43474347 // Y|(X^C) -> (X|Y)^C iff Y&C == 0
43484348 if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) &&
43494349 MaskedValueIsZero(Op0, C1->getValue())) {
4350 Instruction *NOr = BinaryOperator::createOr(A, Op0);
4350 Instruction *NOr = BinaryOperator::CreateOr(A, Op0);
43514351 InsertNewInstBefore(NOr, I);
43524352 NOr->takeName(Op0);
4353 return BinaryOperator::createXor(NOr, C1);
4353 return BinaryOperator::CreateXor(NOr, C1);
43544354 }
43554355
43564356 // (A & C)|(B & D)
44004400
44014401 if (V1) {
44024402 Value *Or =
4403 InsertNewInstBefore(BinaryOperator::createOr(V2, V3, "tmp"), I);
4404 return BinaryOperator::createAnd(V1, Or);
4403 InsertNewInstBefore(BinaryOperator::CreateOr(V2, V3, "tmp"), I);
4404 return BinaryOperator::CreateAnd(V1, Or);
44054405 }
44064406 }
44074407 }
44134413 SI0->getOperand(1) == SI1->getOperand(1) &&
44144414 (SI0->hasOneUse() || SI1->hasOneUse())) {
44154415 Instruction *NewOp =
4416 InsertNewInstBefore(BinaryOperator::createOr(SI0->getOperand(0),
4416 InsertNewInstBefore(BinaryOperator::CreateOr(SI0->getOperand(0),
44174417 SI1->getOperand(0),
44184418 SI0->getName()), I);
4419 return BinaryOperator::create(SI1->getOpcode(), NewOp,
4419 return BinaryOperator::Create(SI1->getOpcode(), NewOp,
44204420 SI1->getOperand(1));
44214421 }
44224422 }
44344434
44354435 // (~A | ~B) == (~(A & B)) - De Morgan's Law
44364436 if (A && isOnlyUse(Op0) && isOnlyUse(Op1)) {
4437 Value *And = InsertNewInstBefore(BinaryOperator::createAnd(A, B,
4437 Value *And = InsertNewInstBefore(BinaryOperator::CreateAnd(A, B,
44384438 I.getName()+".demorgan"), I);
4439 return BinaryOperator::createNot(And);
4439 return BinaryOperator::CreateNot(And);
44404440 }
44414441 }
44424442
44884488 case ICmpInst::ICMP_EQ:
44894489 if (LHSCst == SubOne(RHSCst)) {// (X == 13 | X == 14) -> X-13
44904490 Constant *AddCST = ConstantExpr::getNeg(LHSCst);
4491 Instruction *Add = BinaryOperator::createAdd(LHSVal, AddCST,
4491 Instruction *Add = BinaryOperator::CreateAdd(LHSVal, AddCST,
44924492 LHSVal->getName()+".off");
44934493 InsertNewInstBefore(Add, I);
44944494 AddCST = Subtract(AddOne(RHSCst), LHSCst);
46074607 I.getType(), TD) &&
46084608 ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0),
46094609 I.getType(), TD)) {
4610 Instruction *NewOp = BinaryOperator::createOr(Op0C->getOperand(0),
4610 Instruction *NewOp = BinaryOperator::CreateOr(Op0C->getOperand(0),
46114611 Op1C->getOperand(0),
46124612 I.getName());
46134613 InsertNewInstBefore(NewOp, I);
4614 return CastInst::create(Op0C->getOpcode(), NewOp, I.getType());
4614 return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
46154615 }
46164616 }
46174617 }
46964696 if (dyn_castNotVal(Op0I->getOperand(1))) Op0I->swapOperands();
46974697 if (Value *Op0NotVal = dyn_castNotVal(Op0I->getOperand(0))) {
46984698 Instruction *NotY =
4699 BinaryOperator::createNot(Op0I->getOperand(1),
4699 BinaryOperator::CreateNot(Op0I->getOperand(1),
47004700 Op0I->getOperand(1)->getName()+".not");
47014701 InsertNewInstBefore(NotY, I);
47024702 if (Op0I->getOpcode() == Instruction::And)
4703 return BinaryOperator::createOr(Op0NotVal, NotY);
4703 return BinaryOperator::CreateOr(Op0NotVal, NotY);
47044704 else
4705 return BinaryOperator::createAnd(Op0NotVal, NotY);
4705 return BinaryOperator::CreateAnd(Op0NotVal, NotY);
47064706 }
47074707 }
47084708 }
47284728 Constant *NegOp0I0C = ConstantExpr::getNeg(Op0I0C);
47294729 Constant *ConstantRHS = ConstantExpr::getSub(NegOp0I0C,
47304730 ConstantInt::get(I.getType(), 1));
4731 return BinaryOperator::createAdd(Op0I->getOperand(1), ConstantRHS);
4731 return BinaryOperator::CreateAdd(Op0I->getOperand(1), ConstantRHS);
47324732 }
47334733
47344734 if (ConstantInt *Op0CI = dyn_cast(Op0I->getOperand(1))) {
47364736 // ~(X-c) --> (-c-1)-X
47374737 if (RHS->isAllOnesValue()) {
47384738 Constant *NegOp0CI = ConstantExpr::getNeg(Op0CI);
4739 return BinaryOperator::createSub(
4739 return BinaryOperator::CreateSub(
47404740 ConstantExpr::getSub(NegOp0CI,
47414741 ConstantInt::get(I.getType(), 1)),
47424742 Op0I->getOperand(0));
47434743 } else if (RHS->getValue().isSignBit()) {
47444744 // (X + C) ^ signbit -> (X + C + signbit)
47454745 Constant *C = ConstantInt::get(RHS->getValue() + Op0CI->getValue());
4746 return BinaryOperator::createAdd(Op0I->getOperand(0), C);
4746 return BinaryOperator::CreateAdd(Op0I->getOperand(0), C);
47474747
47484748 }
47494749 } else if (Op0I->getOpcode() == Instruction::Or) {
48194819 std::swap(A, B);
48204820 if (B == Op1) { // (A|B)^B == A & ~B
48214821 Instruction *NotB =
4822 InsertNewInstBefore(BinaryOperator::createNot(Op1, "tmp"), I);
4823 return BinaryOperator::createAnd(A, NotB);
4822 InsertNewInstBefore(BinaryOperator::CreateNot(Op1, "tmp"), I);
4823 return BinaryOperator::CreateAnd(A, NotB);
48244824 }
48254825 } else if (match(Op0I, m_Xor(m_Value(A), m_Value(B)))) {
48264826 if (Op1 == A) // (A^B)^A == B
48334833 if (B == Op1 && // (B&A)^A == ~B & A
48344834 !isa(Op1)) { // Canonical form is (B&C)^C
48354835 Instruction *N =
4836 InsertNewInstBefore(BinaryOperator::createNot(A, "tmp"), I);
4837 return BinaryOperator::createAnd(N, Op1);
4836 InsertNewInstBefore(BinaryOperator::CreateNot(A, "tmp"), I);
4837 return BinaryOperator::CreateAnd(N, Op1);
48384838 }
48394839 }
48404840 }
48454845 Op0I->getOperand(1) == Op1I->getOperand(1) &&
48464846 (Op1I->hasOneUse() || Op1I->hasOneUse())) {
48474847 Instruction *NewOp =
4848 InsertNewInstBefore(BinaryOperator::createXor(Op0I->getOperand(0),
4848 InsertNewInstBefore(BinaryOperator::CreateXor(Op0I->getOperand(0),
48494849 Op1I->getOperand(0),
48504850 Op0I->getName()), I);
4851 return BinaryOperator::create(Op1I->getOpcode(), NewOp,
4851 return BinaryOperator::Create(Op1I->getOpcode(), NewOp,
48524852 Op1I->getOperand(1));
48534853 }
48544854
48584858 if (match(Op0I, m_And(m_Value(A), m_Value(B))) &&
48594859 match(Op1I, m_Or(m_Value(C), m_Value(D)))) {
48604860 if ((A == C && B == D) || (A == D && B == C))
4861 return BinaryOperator::createXor(A, B);
4861 return BinaryOperator::CreateXor(A, B);
48624862 }
48634863 // (A | B)^(A & B) -> A ^ B
48644864 if (match(Op0I, m_Or(m_Value(A), m_Value(B))) &&
48654865 match(Op1I, m_And(m_Value(C), m_Value(D)))) {
48664866 if ((A == C && B == D) || (A == D && B == C))
4867 return BinaryOperator::createXor(A, B);
4867 return BinaryOperator::CreateXor(A, B);
48684868 }
48694869
48704870 // (A & B)^(C & D)
48844884
48854885 if (X) {
48864886 Instruction *NewOp =
4887 InsertNewInstBefore(BinaryOperator::createXor(Y, Z, Op0->getName()), I);
4888 return BinaryOperator::createAnd(NewOp, X);
4887 InsertNewInstBefore(BinaryOperator::CreateXor(Y, Z, Op0->getName()), I);
4888 return BinaryOperator::CreateAnd(NewOp, X);
48894889 }
48904890 }
48914891 }
49064906 I.getType(), TD) &&
49074907 ValueRequiresCast(Op1C->getOpcode(), Op1C->getOperand(0),
49084908 I.getType(), TD)) {
4909 Instruction *NewOp = BinaryOperator::createXor(Op0C->getOperand(0),
4909 Instruction *NewOp = BinaryOperator::CreateXor(Op0C->getOperand(0),
49104910 Op1C->getOperand(0),
49114911 I.getName());
49124912 InsertNewInstBefore(NewOp, I);
4913 return CastInst::create(Op0C->getOpcode(), NewOp, I.getType());
4913 return CastInst::Create(Op0C->getOpcode(), NewOp, I.getType());
49144914 }
49154915 }
49164916 }
49594959 Result = ConstantInt::get(RC->getValue() + APInt(IntPtrWidth, Size));
49604960 else
49614961 Result = IC.InsertNewInstBefore(
4962 BinaryOperator::createAdd(Result,
4962 BinaryOperator::CreateAdd(Result,
49634963 ConstantInt::get(IntPtrTy, Size),
49644964 GEP->getName()+".offs"), I);
49654965 continue;
49734973 else {
49744974 // Emit an add instruction.
49754975 Result = IC.InsertNewInstBefore(
4976 BinaryOperator::createAdd(Result, Scale,
4976 BinaryOperator::CreateAdd(Result, Scale,
49774977 GEP->getName()+".offs"), I);
49784978 }
49794979 continue;
49914991 if (Constant *OpC = dyn_cast(Op))
49924992 Op = ConstantExpr::getMul(OpC, Scale);
49934993 else // We'll let instcombine(mul) convert this to a shl if possible.
4994 Op = IC.InsertNewInstBefore(BinaryOperator::createMul(Op, Scale,
4994 Op = IC.InsertNewInstBefore(BinaryOperator::CreateMul(Op, Scale,
49954995 GEP->getName()+".idx"), I);
49964996 }
49974997
50005000 Result = ConstantExpr::getAdd(cast(Op),
50015001 cast(Result));
50025002 else
5003 Result = IC.InsertNewInstBefore(BinaryOperator::createAdd(Op, Result,
5003 Result = IC.InsertNewInstBefore(BinaryOperator::CreateAdd(Op, Result,
50045004 GEP->getName()+".offs"), I);
50055005 }
50065006 return Result;
51055105 // Okay, we can do this evaluation. Start by converting the index to intptr.
51065106 const Type *IntPtrTy = TD.getIntPtrType();
51075107 if (VariableIdx->getType() != IntPtrTy)
5108 VariableIdx = CastInst::createIntegerCast(VariableIdx, IntPtrTy,
5108 VariableIdx = CastInst::CreateIntegerCast(VariableIdx, IntPtrTy,
51095109 true /*SExt*/,
51105110 VariableIdx->getNameStart(), &I);
51115111 Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs);
5112 return BinaryOperator::createAdd(VariableIdx, OffsetVal, "offset", &I);
5112 return BinaryOperator::CreateAdd(VariableIdx, OffsetVal, "offset", &I);
51135113 }
51145114
51155115
53395339 switch (I.getPredicate()) {
53405340 default: assert(0 && "Invalid icmp instruction!");
53415341 case ICmpInst::ICMP_EQ: { // icmp eq bool %A, %B -> ~(A^B)
5342 Instruction *Xor = BinaryOperator::createXor(Op0, Op1, I.getName()+"tmp");
5342 Instruction *Xor = BinaryOperator::CreateXor(Op0, Op1, I.getName()+"tmp");
53435343 InsertNewInstBefore(Xor, I);
5344 return BinaryOperator::createNot(Xor);
5344 return BinaryOperator::CreateNot(Xor);
53455345 }
53465346 case ICmpInst::ICMP_NE: // icmp eq bool %A, %B -> A^B
5347 return BinaryOperator::createXor(Op0, Op1);
5347 return BinaryOperator::CreateXor(Op0, Op1);
53485348
53495349 case ICmpInst::ICMP_UGT:
53505350 case ICmpInst::ICMP_SGT:
53525352 // FALL THROUGH
53535353 case ICmpInst::ICMP_ULT:
53545354 case ICmpInst::ICMP_SLT: { // icmp lt bool A, B -> ~X & Y
5355 Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
5355 Instruction *Not = BinaryOperator::CreateNot(Op0, I.getName()+"tmp");
53565356 InsertNewInstBefore(Not, I);
5357 return BinaryOperator::createAnd(Not, Op1);
5357 return BinaryOperator::CreateAnd(Not, Op1);
53585358 }
53595359 case ICmpInst::ICMP_UGE:
53605360 case ICmpInst::ICMP_SGE:
53625362 // FALL THROUGH
53635363 case ICmpInst::ICMP_ULE:
53645364 case ICmpInst::ICMP_SLE: { // icmp le bool %A, %B -> ~A | B
5365 Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
5365 Instruction *Not = BinaryOperator::CreateNot(Op0, I.getName()+"tmp");
53665366 InsertNewInstBefore(Not, I);
5367 return BinaryOperator::createOr(Not, Op1);
5367 return BinaryOperator::CreateOr(Not, Op1);
53685368 }
53695369 }
53705370 }
56965696 if (ConstantInt *C2 = dyn_cast(D))
56975697 if (Op1->hasOneUse()) {
56985698 Constant *NC = ConstantInt::get(C1->getValue() ^ C2->getValue());
5699 Instruction *Xor = BinaryOperator::createXor(C, NC, "tmp");
5699 Instruction *Xor = BinaryOperator::CreateXor(C, NC, "tmp");
57005700 return new ICmpInst(I.getPredicate(), A,
57015701 InsertNewInstBefore(Xor, I));
57025702 }
57445744 }
57455745
57465746 if (X) { // Build (X^Y) & Z
5747 Op1 = InsertNewInstBefore(BinaryOperator::createXor(X, Y, "tmp"), I);
5748 Op1 = InsertNewInstBefore(BinaryOperator::createAnd(Op1, Z, "tmp"), I);
5747 Op1 = InsertNewInstBefore(BinaryOperator::CreateXor(X, Y, "tmp"), I);
5748 Op1 = InsertNewInstBefore(BinaryOperator::CreateAnd(Op1, Z, "tmp"), I);
57495749 I.setOperand(0, Op1);
57505750 I.setOperand(1, Constant::getNullValue(Op1->getType()));
57515751 return &I;
59595959 APInt NewCI = RHSV;
59605960 NewCI.zext(BitWidth);
59615961 Instruction *NewAnd =
5962 BinaryOperator::createAnd(Cast->getOperand(0),
5962 BinaryOperator::CreateAnd(Cast->getOperand(0),
59635963 ConstantInt::get(NewCST),LHSI->getName());
59645964 InsertNewInstBefore(NewAnd, ICI);
59655965 return new ICmpInst(ICI.getPredicate(), NewAnd,
60396039 // Compute C << Y.
60406040 Value *NS;
60416041 if (Shift->getOpcode() == Instruction::LShr) {
6042 NS = BinaryOperator::createShl(AndCST,
6042 NS = BinaryOperator::CreateShl(AndCST,
60436043 Shift->getOperand(1), "tmp");
60446044 } else {
60456045 // Insert a logical shift.
6046 NS = BinaryOperator::createLShr(AndCST,
6046 NS = BinaryOperator::CreateLShr(AndCST,
60476047 Shift->getOperand(1), "tmp");
60486048 }
60496049 InsertNewInstBefore(cast(NS), ICI);
60506050
60516051 // Compute X & (C << Y).
60526052 Instruction *NewAnd =
6053 BinaryOperator::createAnd(Shift->getOperand(0), NS, LHSI->getName());
6053 BinaryOperator::CreateAnd(Shift->getOperand(0), NS, LHSI->getName());
60546054 InsertNewInstBefore(NewAnd, ICI);
60556055
60566056 ICI.setOperand(0, NewAnd);
60896089 ConstantInt::get(APInt::getLowBitsSet(TypeBits, TypeBits-ShAmtVal));
60906090
60916091 Instruction *AndI =
6092 BinaryOperator::createAnd(LHSI->getOperand(0),
6092 BinaryOperator::CreateAnd(LHSI->getOperand(0),
60936093 Mask, LHSI->getName()+".mask");
60946094 Value *And = InsertNewInstBefore(AndI, ICI);
60956095 return new ICmpInst(ICI.getPredicate(), And,
61056105 Constant *Mask = ConstantInt::get(APInt(TypeBits, 1) <<
61066106 (TypeBits-ShAmt->getZExtValue()-1));
61076107 Instruction *AndI =
6108 BinaryOperator::createAnd(LHSI->getOperand(0),
6108 BinaryOperator::CreateAnd(LHSI->getOperand(0),
61096109 Mask, LHSI->getName()+".mask");
61106110 Value *And = InsertNewInstBefore(AndI, ICI);
61116111
61606160 Constant *Mask = ConstantInt::get(Val);
61616161
61626162 Instruction *AndI =
6163 BinaryOperator::createAnd(LHSI->getOperand(0),
6163 BinaryOperator::CreateAnd(LHSI->getOperand(0),
61646164 Mask, LHSI->getName()+".mask");
61656165 Value *And = InsertNewInstBefore(AndI, ICI);
61666166 return new ICmpInst(ICI.getPredicate(), And,
62296229 const APInt &V = cast(BO->getOperand(1))->getValue();
62306230 if (V.sgt(APInt(V.getBitWidth(), 1)) && V.isPowerOf2()) {
62316231 Instruction *NewRem =
6232 BinaryOperator::createURem(BO->getOperand(0), BO->getOperand(1),
6232 BinaryOperator::CreateURem(BO->getOperand(0), BO->getOperand(1),
62336233 BO->getName());
62346234 InsertNewInstBefore(NewRem, ICI);
62356235 return new ICmpInst(ICI.getPredicate(), NewRem,
62536253 else if (Value *NegVal = dyn_castNegVal(BOp0))
62546254 return new ICmpInst(ICI.getPredicate(), NegVal, BOp1);
62556255 else if (BO->hasOneUse()) {
6256 Instruction *Neg = BinaryOperator::createNeg(BOp1);
6256 Instruction *Neg = BinaryOperator::CreateNeg(BOp1);
62576257 InsertNewInstBefore(Neg, ICI);
62586258 Neg->takeName(BO);
62596259 return new ICmpInst(ICI.getPredicate(), BOp0, Neg);
64906490 if (Constant *CI = dyn_cast(Result))
64916491 return ReplaceInstUsesWith(ICI, ConstantExpr::getNot(CI));
64926492 else
6493 return BinaryOperator::createNot(Result);
6493 return BinaryOperator::CreateNot(Result);
64946494 }
64956495 }
64966496
65166516 // See if we can turn a signed shr into an unsigned shr.
65176517 if (MaskedValueIsZero(Op0,
65186518 APInt::getSignBit(I.getType()->getPrimitiveSizeInBits())))
6519 return BinaryOperator::createLShr(Op0, I.getOperand(1));
6519 return BinaryOperator::CreateLShr(Op0, I.getOperand(1));
65206520
65216521 return 0;
65226522 }
65846584 if (BinaryOperator *BO = dyn_cast(Op0))
65856585 if (BO->getOpcode() == Instruction::Mul && isLeftShift)
65866586 if (Constant *BOOp = dyn_cast(BO->getOperand(1)))
6587 return BinaryOperator::createMul(BO->getOperand(0),
6587 return BinaryOperator::CreateMul(BO->getOperand(0),
65886588 ConstantExpr::getShl(BOOp, Op1));
65896589
65906590 // Try to fold constant and into select arguments.
66076607 isa(TrOp->getOperand(1))) {
66086608 // Okay, we'll do this xform. Make the shift of shift.
66096609 Constant *ShAmt = ConstantExpr::getZExt(Op1, TrOp->getType());
6610 Instruction *NSh = BinaryOperator::create(I.getOpcode(), TrOp, ShAmt,
6610 Instruction *NSh = BinaryOperator::Create(I.getOpcode(), TrOp, ShAmt,
66116611 I.getName());
66126612 InsertNewInstBefore(NSh, I); // (shift2 (shift1 & 0x00FF), c2)
66136613
66306630 MaskV = MaskV.lshr(Op1->getZExtValue());
66316631 }
66326632
6633 Instruction *And = BinaryOperator::createAnd(NSh, ConstantInt::get(MaskV),
6633 Instruction *And = BinaryOperator::CreateAnd(NSh, ConstantInt::get(MaskV),
66346634 TI->getName());
66356635 InsertNewInstBefore(And, I); // shift1 & 0x00FF
66366636
66556655 if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
66566656 match(Op0BO->getOperand(1),
66576657 m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) {
6658 Instruction *YS = BinaryOperator::createShl(
6658 Instruction *YS = BinaryOperator::CreateShl(
66596659 Op0BO->getOperand(0), Op1,
66606660 Op0BO->getName());
66616661 InsertNewInstBefore(YS, I); // (Y << C)
66626662 Instruction *X =
6663 BinaryOperator::create(Op0BO->getOpcode(), YS, V1,
6663 BinaryOperator::Create(Op0BO->getOpcode(), YS, V1,
66646664 Op0BO->getOperand(1)->getName());
66656665 InsertNewInstBefore(X, I); // (X + (Y << C))
66666666 uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
6667 return BinaryOperator::createAnd(X, ConstantInt::get(
6667 return BinaryOperator::CreateAnd(X, ConstantInt::get(
66686668 APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
66696669 }
66706670
66756675 m_And(m_Shr(m_Value(V1), m_Value(V2)),m_ConstantInt(CC))) &&
66766676 cast(Op0BOOp1)->getOperand(0)->hasOneUse() &&
66776677 V2 == Op1) {
6678 Instruction *YS = BinaryOperator::createShl(
6678 Instruction *YS = BinaryOperator::CreateShl(
66796679 Op0BO->getOperand(0), Op1,
66806680 Op0BO->getName());
66816681 InsertNewInstBefore(YS, I); // (Y << C)
66826682 Instruction *XM =
6683 BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1),
6683 BinaryOperator::CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
66846684 V1->getName()+".mask");
66856685 InsertNewInstBefore(XM, I); // X & (CC << C)
66866686
6687 return BinaryOperator::create(Op0BO->getOpcode(), YS, XM);
6687 return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
66886688 }
66896689 }
66906690
66946694 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
66956695 match(Op0BO->getOperand(0),
66966696 m_Shr(m_Value(V1), m_ConstantInt(CC))) && CC == Op1) {
6697 Instruction *YS = BinaryOperator::createShl(
6697 Instruction *YS = BinaryOperator::CreateShl(
66986698 Op0BO->getOperand(1), Op1,
66996699 Op0BO->getName());
67006700 InsertNewInstBefore(YS, I); // (Y << C)
67016701 Instruction *X =
6702 BinaryOperator::create(Op0BO->getOpcode(), V1, YS,
6702 BinaryOperator::Create(Op0BO->getOpcode(), V1, YS,
67036703 Op0BO->getOperand(0)->getName());
67046704 InsertNewInstBefore(X, I); // (X + (Y << C))
67056705 uint32_t Op1Val = Op1->getLimitedValue(TypeBits);
6706 return BinaryOperator::createAnd(X, ConstantInt::get(
6706 return BinaryOperator::CreateAnd(X, ConstantInt::get(
67076707 APInt::getHighBitsSet(TypeBits, TypeBits-Op1Val)));
67086708 }
67096709
67146714 m_ConstantInt(CC))) && V2 == Op1 &&
67156715 cast(Op0BO->getOperand(0))
67166716 ->getOperand(0)->hasOneUse()) {
6717 Instruction *YS = BinaryOperator::createShl(
6717 Instruction *YS = BinaryOperator::CreateShl(
67186718 Op0BO->getOperand(1), Op1,
67196719 Op0BO->getName());
67206720 InsertNewInstBefore(YS, I); // (Y << C)
67216721 Instruction *XM =
6722 BinaryOperator::createAnd(V1, ConstantExpr::getShl(CC, Op1),
6722 BinaryOperator::CreateAnd(V1, ConstantExpr::getShl(CC, Op1),
67236723 V1->getName()+".mask");
67246724 InsertNewInstBefore(XM, I); // X & (CC << C)
67256725
6726 return BinaryOperator::create(Op0BO->getOpcode(), XM, YS);
6726 return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
67276727 }
67286728
67296729 break;
67646764 Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1);
67656765
67666766 Instruction *NewShift =
6767 BinaryOperator::create(I.getOpcode(), Op0BO->getOperand(0), Op1);
6767 BinaryOperator::Create(I.getOpcode(), Op0BO->getOperand(0), Op1);
67686768 InsertNewInstBefore(NewShift, I);
67696769 NewShift->takeName(Op0BO);
67706770
6771 return BinaryOperator::create(Op0BO->getOpcode(), NewShift,
6771 return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
67726772 NewRHS);
67736773 }
67746774 }
67966796
67976797 // Check for (X << c1) << c2 and (X >> c1) >> c2
67986798 if (I.getOpcode() == ShiftOp->getOpcode()) {
6799 return BinaryOperator::create(I.getOpcode(), X,
6799 return BinaryOperator::Create(I.getOpcode(), X,
68006800 ConstantInt::get(Ty, AmtSum));
68016801 } else if (ShiftOp->getOpcode() == Instruction::LShr &&
68026802 I.getOpcode() == Instruction::AShr) {
68036803 // ((X >>u C1) >>s C2) -> (X >>u (C1+C2)) since C1 != 0.
6804 return BinaryOperator::createLShr(X, ConstantInt::get(Ty, AmtSum));
6804 return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum));
68056805 } else if (ShiftOp->getOpcode() == Instruction::AShr &&
68066806 I.getOpcode() == Instruction::LShr) {
68076807 // ((X >>s C1) >>u C2) -> ((X >>s (C1+C2)) & mask) since C1 != 0.
68086808 Instruction *Shift =
6809 BinaryOperator::createAShr(X, ConstantInt::get(Ty, AmtSum));
6809 BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum));
68106810 InsertNewInstBefore(Shift, I);
68116811
68126812 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
6813 return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask));
6813 return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask));
68146814 }
68156815
68166816 // Okay, if we get here, one shift must be left, and the other shift must be
68196819 // If we have ((X >>? C) << C), turn this into X & (-1 << C).
68206820 if (I.getOpcode() == Instruction::Shl) {
68216821 APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt1));
6822 return BinaryOperator::createAnd(X, ConstantInt::get(Mask));
6822 return BinaryOperator::CreateAnd(X, ConstantInt::get(Mask));
68236823 }
68246824 // If we have ((X << C) >>u C), turn this into X & (-1 >>u C).
68256825 if (I.getOpcode() == Instruction::LShr) {
68266826 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1));
6827 return BinaryOperator::createAnd(X, ConstantInt::get(Mask));
6827 return BinaryOperator::CreateAnd(X, ConstantInt::get(Mask));
68286828 }
68296829 // We can simplify ((X << C) >>s C) into a trunc + sext.
68306830 // NOTE: we could do this for any C, but that would make 'unusual' integer
68566856 assert(ShiftOp->getOpcode() == Instruction::LShr ||
68576857 ShiftOp->getOpcode() == Instruction::AShr);
68586858 Instruction *Shift =
6859 BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff));
6859 BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff));
68606860 InsertNewInstBefore(Shift, I);
68616861
68626862 APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
6863 return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask));
6863 return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask));
68646864 }
68656865
68666866 // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2)
68676867 if (I.getOpcode() == Instruction::LShr) {
68686868 assert(ShiftOp->getOpcode() == Instruction::Shl);
68696869 Instruction *Shift =
6870 BinaryOperator::createLShr(X, ConstantInt::get(Ty, ShiftDiff));
6870 BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, ShiftDiff));
68716871 InsertNewInstBefore(Shift, I);
68726872
68736873 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
6874 return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask));
6874 return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask));
68756875 }
68766876
68776877 // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in.
68846884 assert(ShiftOp->getOpcode() == Instruction::LShr ||
68856885 ShiftOp->getOpcode() == Instruction::AShr);
68866886 Instruction *Shift =
6887 BinaryOperator::create(ShiftOp->getOpcode(), X,
6887 BinaryOperator::Create(ShiftOp->getOpcode(), X,
68886888 ConstantInt::get(Ty, ShiftDiff));
68896889 InsertNewInstBefore(Shift, I);
68906890
68916891 APInt Mask(APInt::getHighBitsSet(TypeBits, TypeBits - ShiftAmt2));
6892 return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask));
6892 return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask));
68936893 }
68946894
68956895 // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2)
68966896 if (I.getOpcode() == Instruction::LShr) {
68976897 assert(ShiftOp->getOpcode() == Instruction::Shl);
68986898 Instruction *Shift =
6899 BinaryOperator::createShl(X, ConstantInt::get(Ty, ShiftDiff));
6899 BinaryOperator::CreateShl(X, ConstantInt::get(Ty, ShiftDiff));
69006900 InsertNewInstBefore(Shift, I);
69016901
69026902 APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2));
6903 return BinaryOperator::createAnd(Shift, ConstantInt::get(Mask));
6903 return BinaryOperator::CreateAnd(Shift, ConstantInt::get(Mask));
69046904 }
69056905
69066906 // We can't handle (X << C1) >>a C2, it shifts arbitrary bits in.
70157015 Amt = Multiply(cast(NumElements), cast(Amt));
70167016 // otherwise multiply the amount and the number of elements
70177017 else if (Scale != 1) {
7018 Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp");
7018 Instruction *Tmp = BinaryOperator::CreateMul(Amt, NumElements, "tmp");
70197019 Amt = InsertNewInstBefore(Tmp, AI);
70207020 }
70217021 }
70227022
70237023 if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
70247024 Value *Off = ConstantInt::get(Type::Int32Ty, Offset, true);
7025 Instruction *Tmp = BinaryOperator::createAdd(Amt, Off, "tmp");
7025 Instruction *Tmp = BinaryOperator::CreateAdd(Amt, Off, "tmp");
70267026 Amt = InsertNewInstBefore(Tmp, AI);
70277027 }
70287028
71747174 case Instruction::Shl: {
71757175 Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned);
71767176 Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned);
7177 Res = BinaryOperator::create((Instruction::BinaryOps)I->getOpcode(),
7177 Res = BinaryOperator::Create((Instruction::BinaryOps)I->getOpcode(),
71787178 LHS, RHS, I->getName());
71797179 break;
71807180 }
71887188 return I->getOperand(0);
71897189
71907190 // Otherwise, must be the same type of case, so just reinsert a new one.
7191 Res = CastInst::create(cast(I)->getOpcode(), I->getOperand(0),
7191 Res = CastInst::Create(cast(I)->getOpcode(), I->getOperand(0),
71927192 Ty, I->getName());
71937193 break;
71947194 default:
72117211 isEliminableCastPair(CSrc, CI.getOpcode(), CI.getType(), TD)) {
72127212 // The first cast (CSrc) is eliminable so we need to fix up or replace
72137213 // the second cast (CI). CSrc will then have a good chance of being dead.
7214 return CastInst::create(opc, CSrc->getOperand(0), CI.getType());
7214 return CastInst::Create(opc, CSrc->getOperand(0), CI.getType());
72157215 }
72167216 }
72177217
74067406 assert(SrcBitSize < DestBitSize && "Not a zext?");
74077407 Constant *C = ConstantInt::get(APInt::getLowBitsSet(DestBitSize,
74087408 SrcBitSize));
7409 return BinaryOperator::createAnd(Res, C);
7409 return BinaryOperator::CreateAnd(Res, C);
74107410 }
74117411 case Instruction::SExt:
74127412 // We need to emit a cast to truncate, then a cast to sext.
7413 return CastInst::create(Instruction::SExt,
7413 return CastInst::Create(Instruction::SExt,
74147414 InsertCastBefore(Instruction::Trunc, Res, Src->getType(),
74157415 CI), DestTy);
74167416 }
74377437 Instruction::CastOps opcode = CI.getOpcode();
74387438 Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI);
74397439 Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI);
7440 return BinaryOperator::create(
7440 return BinaryOperator::Create(
74417441 cast(SrcI)->getOpcode(), Op0c, Op1c);
74427442 }
74437443 }
74487448 Op1 == ConstantInt::getTrue() &&
74497449 (!Op0->hasOneUse() || !isa(Op0))) {
74507450 Value *New = InsertOperandCastBefore(Instruction::ZExt, Op0, DestTy, &CI);
7451 return BinaryOperator::createXor(New, ConstantInt::get(CI.getType(), 1));
7451 return BinaryOperator::CreateXor(New, ConstantInt::get(CI.getType(), 1));
74527452 }
74537453 break;
74547454 case Instruction::SDiv:
74667466 Op0, DestTy, SrcI);
74677467 Value *Op1c = InsertOperandCastBefore(Instruction::BitCast,
74687468 Op1, DestTy, SrcI);
7469 return BinaryOperator::create(
7469 return BinaryOperator::Create(
74707470 cast(SrcI)->getOpcode(), Op0c, Op1c);
74717471 }
74727472 }
74847484 Instruction::BitCast : Instruction::Trunc);
74857485 Value *Op0c = InsertOperandCastBefore(opcode, Op0, DestTy, SrcI);
74867486 Value *Op1c = InsertOperandCastBefore(opcode, Op1, DestTy, SrcI);
7487 return BinaryOperator::createShl(Op0c, Op1c);
7487 return BinaryOperator::CreateShl(Op0c, Op1c);
74887488 }
74897489 break;
74907490 case Instruction::AShr:
74967496 uint32_t ShiftAmt = cast(Op1)->getLimitedValue(SrcBitSize);
74977497 if (SrcBitSize > ShiftAmt && SrcBitSize-ShiftAmt >= DestBitSize) {
74987498 // Insert the new logical shift right.
7499 return BinaryOperator::createLShr(Op0, Op1);
7499 return BinaryOperator::CreateLShr(Op0, Op1);
75007500 }
75017501 }
75027502 break;
75347534 Value *V1 = InsertCastBefore(Instruction::Trunc, SrcIOp0, Ty, CI);
75357535 Value *V2 = InsertCastBefore(Instruction::Trunc, SrcI->getOperand(1),
75367536 Ty, CI);
7537 return BinaryOperator::createLShr(V1, V2);
7537 return BinaryOperator::CreateLShr(V1, V2);
75387538 }
75397539 } else { // This is a variable shr.
75407540
75457545 Value *One = ConstantInt::get(SrcI->getType(), 1);
75467546
75477547 Value *V = InsertNewInstBefore(
7548 BinaryOperator::createShl(One, SrcI->getOperand(1),
7548 BinaryOperator::CreateShl(One, SrcI->getOperand(1),
75497549 "tmp"), CI);
7550 V = InsertNewInstBefore(BinaryOperator::createAnd(V,
7550 V = InsertNewInstBefore(BinaryOperator::CreateAnd(V,
75517551 SrcI->getOperand(0),
75527552 "tmp"), CI);
75537553 Value *Zero = Constant::getNullValue(V->getType());
75807580 Value *In = ICI->getOperand(0);
75817581 Value *Sh = ConstantInt::get(In->getType(),
75827582 In->getType()->getPrimitiveSizeInBits()-1);
7583 In = InsertNewInstBefore(BinaryOperator::createLShr(In, Sh,
7583 In = InsertNewInstBefore(BinaryOperator::CreateLShr(In, Sh,
75847584 In->getName()+".lobit"),
75857585 CI);
75867586 if (In->getType() != CI.getType())
7587 In = CastInst::createIntegerCast(In, CI.getType(),
7587 In = CastInst::CreateIntegerCast(In, CI.getType(),
75887588 false/*ZExt*/, "tmp", &CI);
75897589
75907590 if (ICI->getPredicate() == ICmpInst::ICMP_SGT) {
75917591 Constant *One = ConstantInt::get(In->getType(), 1);
7592 In = InsertNewInstBefore(BinaryOperator::createXor(In, One,
7592 In = InsertNewInstBefore(BinaryOperator::CreateXor(In, One,
75937593 In->getName()+".not"),
75947594 CI);
75957595 }
76347634 if (ShiftAmt) {
76357635 // Perform a logical shr by shiftamt.
76367636 // Insert the shift to put the result in the low bit.
7637 In = InsertNewInstBefore(BinaryOperator::createLShr(In,
7637 In = InsertNewInstBefore(BinaryOperator::CreateLShr(In,
76387638 ConstantInt::get(In->getType(), ShiftAmt),
76397639 In->getName()+".lobit"), CI);
76407640 }
76417641
76427642 if ((Op1CV != 0) == isNE) { // Toggle the low bit.
76437643 Constant *One = ConstantInt::get(In->getType(), 1);
7644 In = BinaryOperator::createXor(In, One, "tmp");
7644 In = BinaryOperator::CreateXor(In, One, "tmp");
76457645 InsertNewInstBefore(cast(In), CI);
76467646 }
76477647
76487648 if (CI.getType() == In->getType())
76497649 return ReplaceInstUsesWith(CI, In);
76507650 else
7651 return CastInst::createIntegerCast(In, CI.getType(), false/*ZExt*/);
7651 return CastInst::CreateIntegerCast(In, CI.getType(), false/*ZExt*/);
76527652 }
76537653 }
76547654 }
76807680 APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize));
76817681 Constant *AndConst = ConstantInt::get(AndValue);
76827682 Instruction *And =
7683 BinaryOperator::createAnd(CSrc->getOperand(0), AndConst);
7683 BinaryOperator::CreateAnd(CSrc->getOperand(0), AndConst);
76847684 // Unfortunately, if the type changed, we need to cast it back.
76857685 if (And->getType() != CI.getType()) {
76867686 And->setName(CSrc->getName()+".mask");
76877687 InsertNewInstBefore(And, CI);
7688 And = CastInst::createIntegerCast(And, CI.getType(), false/*ZExt*/);
7688 And = CastInst::CreateIntegerCast(And, CI.getType(), false/*ZExt*/);
76897689 }
76907690 return And;
76917691 }
77067706 transformZExtICmp(RHS, CI, false))) {
77077707 Value *LCast = InsertCastBefore(Instruction::ZExt, LHS, CI.getType(), CI);
77087708 Value *RCast = InsertCastBefore(Instruction::ZExt, RHS, CI.getType(), CI);
7709 return BinaryOperator::create(Instruction::Or, LCast, RCast);
7709 return BinaryOperator::Create(Instruction::Or, LCast, RCast);
77107710 }
77117711 }
77127712
77357735 Value *In = ICI->getOperand(0);
77367736 Value *Sh = ConstantInt::get(In->getType(),
77377737 In->getType()->getPrimitiveSizeInBits()-1);
7738 In = InsertNewInstBefore(BinaryOperator::createAShr(In, Sh,
7738 In = InsertNewInstBefore(BinaryOperator::CreateAShr(In, Sh,
77397739 In->getName()+".lobit"),
77407740 CI);
77417741 if (In->getType() != CI.getType())
7742 In = CastInst::createIntegerCast(In, CI.getType(),
7742 In = CastInst::CreateIntegerCast(In, CI.getType(),
77437743 true/*SExt*/, "tmp", &CI);
77447744
77457745 if (ICI->getPredicate() == ICmpInst::ICMP_SGT)
7746 In = InsertNewInstBefore(BinaryOperator::createNot(In,
7746 In = InsertNewInstBefore(BinaryOperator::CreateNot(In,
77477747 In->getName()+".not"), CI);
77487748
77497749 return ReplaceInstUsesWith(CI, In);
78207820 CI.getType(), CI);
78217821 RHSTrunc = InsertCastBefore(Instruction::FPExt, RHSTrunc,
78227822 CI.getType(), CI);
7823 return BinaryOperator::create(OpI->getOpcode(), LHSTrunc, RHSTrunc);
7823 return BinaryOperator::Create(OpI->getOpcode(), LHSTrunc, RHSTrunc);
78247824 }
78257825 }
78267826 break;
80638063 SelectInst *NewSI = SelectInst::Create(SI.getCondition(), TI->getOperand(0),
80648064 FI->getOperand(0), SI.getName()+".v");
80658065 InsertNewInstBefore(NewSI, SI);
8066 return CastInst::create(Instruction::CastOps(TI->getOpcode()), NewSI,
8066 return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI,
80678067 TI->getType());
80688068 }
80698069
81078107
81088108 if (BinaryOperator *BO = dyn_cast(TI)) {
81098109 if (MatchIsOpZero)
8110 return BinaryOperator::create(BO->getOpcode(), MatchOp, NewSI);
8110 return BinaryOperator::Create(BO->getOpcode(), MatchOp, NewSI);
81118111 else
8112 return BinaryOperator::create(BO->getOpcode(), NewSI, MatchOp);
8112 return BinaryOperator::Create(BO->getOpcode(), NewSI, MatchOp);
81138113 }
81148114 assert(0 && "Shouldn't get here");
81158115 return 0;
81448144 if (ConstantInt *C = dyn_cast(TrueVal)) {
81458145 if (C->getZExtValue()) {
81468146 // Change: A = select B, true, C --> A = or B, C
8147 return BinaryOperator::createOr(CondVal, FalseVal);
8147 return BinaryOperator::CreateOr(CondVal, FalseVal);
81488148 } else {
81498149 // Change: A = select B, false, C --> A = and !B, C
81508150 Value *NotCond =
8151 InsertNewInstBefore(BinaryOperator::createNot(CondVal,
8151 InsertNewInstBefore(BinaryOperator::CreateNot(CondVal,
81528152 "not."+CondVal->getName()), SI);
8153 return BinaryOperator::createAnd(NotCond, FalseVal);
8153 return BinaryOperator::CreateAnd(NotCond, FalseVal);
81548154 }
81558155 } else if (ConstantInt *C = dyn_cast(FalseVal)) {
81568156 if (C->getZExtValue() == false) {
81578157 // Change: A = select B, C, false --> A = and B, C
8158 return BinaryOperator::createAnd(CondVal, TrueVal);
8158 return BinaryOperator::CreateAnd(CondVal, TrueVal);
81598159 } else {
81608160 // Change: A = select B, C, true --> A = or !B, C
81618161 Value *NotCond =
8162 InsertNewInstBefore(BinaryOperator::createNot(CondVal,
8162 InsertNewInstBefore(BinaryOperator::CreateNot(CondVal,
81638163 "not."+CondVal->getName()), SI);
8164 return BinaryOperator::createOr(NotCond, TrueVal);
8164 return BinaryOperator::CreateOr(NotCond, TrueVal);
81658165 }
81668166 }
81678167
81688168 // select a, b, a -> a&b
81698169 // select a, a, b -> a|b
81708170 if (CondVal == TrueVal)
8171 return BinaryOperator::createOr(CondVal, FalseVal);
8171 return BinaryOperator::CreateOr(CondVal, FalseVal);
81728172 else if (CondVal == FalseVal)
8173 return BinaryOperator::createAnd(CondVal, TrueVal);
8173 return BinaryOperator::CreateAnd(CondVal, TrueVal);
81748174 }
81758175
81768176 // Selecting between two integer constants?
81788178 if (ConstantInt *FalseValC = dyn_cast(FalseVal)) {
81798179 // select C, 1, 0 -> zext C to int
81808180 if (FalseValC->isZero() && TrueValC->getValue() == 1) {
8181 return CastInst::create(Instruction::ZExt, CondVal, SI.getType());
8181 return CastInst::Create(Instruction::ZExt, CondVal, SI.getType());
81828182 } else if (TrueValC->isZero() && FalseValC->getValue() == 1) {
81838183 // select C, 0, 1 -> zext !C to int
81848184 Value *NotCond =
8185 InsertNewInstBefore(BinaryOperator::createNot(CondVal,
8185 InsertNewInstBefore(BinaryOperator::CreateNot(CondVal,
81868186 "not."+CondVal->getName()), SI);
8187 return CastInst::create(Instruction::ZExt, NotCond, SI.getType());
8187 return CastInst::Create(Instruction::ZExt, NotCond, SI.getType());
81888188 }
81898189
81908190 // FIXME: Turn select 0/-1 and -1/0 into sext from condition!
82008200 Value *X = IC->getOperand(0);
82018201 uint32_t Bits = X->getType()->getPrimitiveSizeInBits();
82028202 Constant *ShAmt = ConstantInt::get(X->getType(), Bits-1);
8203 Instruction *SRA = BinaryOperator::create(Instruction::AShr, X,
8203 Instruction *SRA = BinaryOperator::Create(Instruction::AShr, X,
82048204 ShAmt, "ones");
82058205 InsertNewInstBefore(SRA, SI);
82068206
82138213 opc = Instruction::SExt;
82148214 else if (SRASize > SISize)
82158215 opc = Instruction::Trunc;
8216 return CastInst::create(opc, SRA, SI.getType());
8216 return CastInst::Create(opc, SRA, SI.getType());
82178217 }
82188218 }
82198219
82388238 ShouldNotVal ^= IC->getPredicate() == ICmpInst::ICMP_NE;
82398239 Value *V = ICA;
82408240 if (ShouldNotVal)
8241 V = InsertNewInstBefore(BinaryOperator::create(
8241 V = InsertNewInstBefore(BinaryOperator::Create(
82428242 Instruction::Xor, V, ICA->getOperand(1)), SI);
82438243 return ReplaceInstUsesWith(SI, V);
82448244 }
83438343 NegVal = ConstantExpr::getNeg(C);
83448344 } else {
83458345 NegVal = InsertNewInstBefore(
8346 BinaryOperator::createNeg(SubOp->getOperand(1), "tmp"), SI);
8346 BinaryOperator::CreateNeg(SubOp->getOperand(1), "tmp"), SI);
83478347 }
83488348
83498349 Value *NewTrueOp = OtherAddOp;
83558355 NewFalseOp, SI.getName() + ".p");
83568356
83578357 NewSel = InsertNewInstBefore(NewSel, SI);
8358 return BinaryOperator::createAdd(SubOp->getOperand(0), NewSel);
8358 return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel);
83598359 }
83608360 }
83618361 }
83838383 InsertNewInstBefore(NewSel, SI);
83848384 NewSel->takeName(TVI);
83858385 if (BinaryOperator *BO = dyn_cast(TVI))
8386 return BinaryOperator::create(BO->getOpcode(), FalseVal, NewSel);
8386 return BinaryOperator::Create(BO->getOpcode(), FalseVal, NewSel);
83878387 else {
83888388 assert(0 && "Unknown instruction!!");
83898389 }
84098409 InsertNewInstBefore(NewSel, SI);
84108410 NewSel->takeName(FVI);
84118411 if (BinaryOperator *BO = dyn_cast(FVI))
8412 return BinaryOperator::create(BO->getOpcode(), TrueVal, NewSel);
8412 return BinaryOperator::Create(BO->getOpcode(), TrueVal, NewSel);
84138413 else
84148414 assert(0 && "Unknown instruction!!");
84158415 }
87678767 i, "tmp");
87688768 InsertNewInstBefore(cast(Result), CI);
87698769 }
8770 return CastInst::create(Instruction::BitCast, Result, CI.getType());
8770 return CastInst::Create(Instruction::BitCast, Result, CI.getType());
87718771 }
87728772 }
87738773 break;
90489048 } else {
90499049 Instruction::CastOps opcode = CastInst::getCastOpcode(*AI,
90509050 false, ParamTy, false);
9051 CastInst *NewCast = CastInst::create(opcode, *AI, ParamTy, "tmp");
9051 CastInst *NewCast = CastInst::Create(opcode, *AI, ParamTy, "tmp");
90529052 Args.push_back(InsertNewInstBefore(NewCast, *Caller));
90539053 }
90549054
90759075 // Must promote to pass through va_arg area!
90769076 Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, false,
90779077 PTy, false);
9078 Instruction *Cast = CastInst::create(opcode, *AI, PTy, "tmp");
9078 Instruction *Cast = CastInst::Create(opcode, *AI, PTy, "tmp");
90799079 InsertNewInstBefore(Cast, *Caller);
90809080 Args.push_back(Cast);
90819081 } else {
91179117 if (NV->getType() != Type::VoidTy) {
91189118 Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false,
91199119 OldRetTy, false);
9120 NV = NC = CastInst::create(opcode, NC, OldRetTy, "tmp");
9120 NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp");
91219121
91229122 // If this is an invoke instruction, we should insert it after the first
91239123 // non-phi, instruction in the normal successor block.
93699369 }
93709370
93719371 if (BinaryOperator *BinOp = dyn_cast(FirstInst))
9372 return BinaryOperator::create(BinOp->getOpcode(), LHSVal, RHSVal);
9372 return BinaryOperator::Create(BinOp->getOpcode(), LHSVal, RHSVal);
93739373 else if (CmpInst *CIOp = dyn_cast(FirstInst))
9374 return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal,
9374 return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(), LHSVal,
93759375 RHSVal);
93769376 else {
93779377 assert(isa(FirstInst));
95149514
95159515 // Insert and return the new operation.
95169516 if (CastInst* FirstCI = dyn_cast(FirstInst))
9517 return CastInst::create(FirstCI->getOpcode(), PhiVal, PN.getType());
9517 return CastInst::Create(FirstCI->getOpcode(), PhiVal, PN.getType());
95189518 if (BinaryOperator *BinOp = dyn_cast(FirstInst))
9519 return BinaryOperator::create(BinOp->getOpcode(), PhiVal, ConstantOp);
9519 return BinaryOperator::Create(BinOp->getOpcode(), PhiVal, ConstantOp);
95209520 if (CmpInst *CIOp = dyn_cast(FirstInst))
9521 return CmpInst::create(CIOp->getOpcode(), CIOp->getPredicate(),
9521 return CmpInst::Create(CIOp->getOpcode(), CIOp->getPredicate(),
95229522 PhiVal, ConstantOp);
95239523 assert(isa(FirstInst) && "Unknown operation");
95249524
98139813 if (isa(SO1) && isa(GO1))
98149814 Sum = ConstantExpr::getAdd(cast(SO1), cast(GO1));
98159815 else {
9816 Sum = BinaryOperator::createAdd(SO1, GO1, PtrOp->getName()+".sum");
9816 Sum = BinaryOperator::CreateAdd(SO1, GO1, PtrOp->getName()+".sum");
98179817 InsertNewInstBefore(cast(Sum), GEP);
98189818 }
98199819 }
99449944 if (Scale->getZExtValue() != 1) {
99459945 Constant *C = ConstantExpr::getIntegerCast(Scale, NewIdx->getType(),
99469946 false /*ZExt*/);
9947 Instruction *Sc = BinaryOperator::createMul(NewIdx, C, "idxscale");
9947 Instruction *Sc = BinaryOperator::CreateMul(NewIdx, C, "idxscale");
99489948 NewIdx = InsertNewInstBefore(Sc, GEP);
99499949 }
99509950
1037810378 NewCast = ConstantExpr::getCast(opcode, C, CastDstTy);
1037910379 else
1038010380 NewCast = IC.InsertNewInstBefore(
10381 CastInst::create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"),
10381 CastInst::Create(opcode, SIOp0, CastDstTy, SIOp0->getName()+".c"),
1038210382 SI);
1038310383 return new StoreInst(NewCast, CastOp);
1038410384 }
1086710867 EI.getName()+".rhs");
1086810868 InsertNewInstBefore(newEI0, EI);
1086910869 InsertNewInstBefore(newEI1, EI);
10870 return BinaryOperator::create(BO->getOpcode(), newEI0, newEI1);
10870 return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1);
1087110871 }
1087210872 } else if (isa(I)) {
1087310873 unsigned AS =
580580 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
581581 SD.SplitValue, ExitValue, "lisplit",
582582 Terminator);
583 Instruction *NSplitCond = BinaryOperator::createAnd(C1, C2, "lisplit",
583 Instruction *NSplitCond = BinaryOperator::CreateAnd(C1, C2, "lisplit",
584584 Terminator);
585585 SD.SplitCondition->replaceAllUsesWith(NSplitCond);
586586 SD.SplitCondition->eraseFromParent();
768768 //
769769 if (ExitCondition->getPredicate() == ICmpInst::ICMP_SLT
770770 || ExitCondition->getPredicate() == ICmpInst::ICMP_ULT) {
771 Value *A = BinaryOperator::createAdd(NV, ConstantInt::get(Ty, 1, Sign),
771 Value *A = BinaryOperator::CreateAdd(NV, ConstantInt::get(Ty, 1, Sign),
772772 "lsplit.add", PHTerminator);
773773 Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,