llvm.org GIT mirror llvm / 551754c
Revert 101465, it broke internal OpenGL testing. Probably the best way to know that all getOperand() calls have been handled is to replace that API instead of updating. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@101579 91177308-0d34-0410-b5e6-96231b3b80d8 Eric Christopher 9 years ago
44 changed file(s) with 599 addition(s) and 590 deletion(s). Raw diff Collapse all Expand all
10301030 /// indirect function invocation.
10311031 ///
10321032 Function *getCalledFunction() const {
1033 return dyn_cast(Op<-1>());
1033 return dyn_cast(Op<0>());
10341034 }
10351035
10361036 /// getCalledValue - Get a pointer to the function that is invoked by this
10371037 /// instruction.
1038 const Value *getCalledValue() const { return Op<-1>(); }
1039 Value *getCalledValue() { return Op<-1>(); }
1038 const Value *getCalledValue() const { return Op<0>(); }
1039 Value *getCalledValue() { return Op<0>(); }
10401040
10411041 /// setCalledFunction - Set the function called.
10421042 void setCalledFunction(Value* Fn) {
1043 Op<-1>() = Fn;
1043 Op<0>() = Fn;
10441044 }
10451045
10461046 // Methods for support type inquiry through isa, cast, and dyn_cast:
10701070 ->getElementType())->getReturnType(),
10711071 Instruction::Call,
10721072 OperandTraits::op_end(this) - (ArgEnd - ArgBegin + 1),
1073 unsigned(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1073 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
10741074 init(Func, ArgBegin, ArgEnd, NameStr,
10751075 typename std::iterator_traits::iterator_category());
10761076 }
4242 Intrinsic::ID getIntrinsicID() const {
4343 return (Intrinsic::ID)getCalledFunction()->getIntrinsicID();
4444 }
45
45
4646 // Methods for support type inquiry through isa, cast, and dyn_cast:
4747 static inline bool classof(const IntrinsicInst *) { return true; }
4848 static inline bool classof(const CallInst *I) {
7373 static inline bool classof(const Value *V) {
7474 return isa(V) && classof(cast(V));
7575 }
76
76
7777 static Value *StripCast(Value *C);
7878 };
7979
8282 class DbgDeclareInst : public DbgInfoIntrinsic {
8383 public:
8484 Value *getAddress() const;
85 MDNode *getVariable() const { return cast(getOperand(1)); }
85 MDNode *getVariable() const { return cast(getOperand(2)); }
8686
8787 // Methods for support type inquiry through isa, cast, and dyn_cast:
8888 static inline bool classof(const DbgDeclareInst *) { return true; }
102102 Value *getValue();
103103 uint64_t getOffset() const {
104104 return cast(
105 const_cast(getOperand(1)))->getZExtValue();
106 }
107 MDNode *getVariable() const { return cast(getOperand(2)); }
105 const_cast(getOperand(2)))->getZExtValue();
106 }
107 MDNode *getVariable() const { return cast(getOperand(3)); }
108108
109109 // Methods for support type inquiry through isa, cast, and dyn_cast:
110110 static inline bool classof(const DbgValueInst *) { return true; }
120120 ///
121121 class MemIntrinsic : public IntrinsicInst {
122122 public:
123 Value *getRawDest() const { return const_cast(getOperand(0)); }
124
125 Value *getLength() const { return const_cast(getOperand(2)); }
123 Value *getRawDest() const { return const_cast(getOperand(1)); }
124
125 Value *getLength() const { return const_cast(getOperand(3)); }
126126 ConstantInt *getAlignmentCst() const {
127 return cast(const_cast(getOperand(3)));
128 }
129
127 return cast(const_cast(getOperand(4)));
128 }
129
130130 unsigned getAlignment() const {
131131 return getAlignmentCst()->getZExtValue();
132132 }
133133
134134 ConstantInt *getVolatileCst() const {
135 return cast(const_cast(getOperand(4)));
135 return cast(const_cast(getOperand(5)));
136136 }
137137 bool isVolatile() const {
138138 return getVolatileCst()->getZExtValue() != 0;
148148 void setDest(Value *Ptr) {
149149 assert(getRawDest()->getType() == Ptr->getType() &&
150150 "setDest called with pointer of wrong type!");
151 setOperand(0, Ptr);
151 setOperand(1, Ptr);
152152 }
153153
154154 void setLength(Value *L) {
155155 assert(getLength()->getType() == L->getType() &&
156156 "setLength called with value of wrong type!");
157 setOperand(2, L);
158 }
159
157 setOperand(3, L);
158 }
159
160160 void setAlignment(Constant* A) {
161 setOperand(3, A);
161 setOperand(4, A);
162162 }
163163
164164 void setVolatile(Constant* V) {
165 setOperand(4, V);
165 setOperand(5, V);
166166 }
167167
168168 const Type *getAlignmentType() const {
169 return getOperand(3)->getType();
170 }
171
169 return getOperand(4)->getType();
170 }
171
172172 // Methods for support type inquiry through isa, cast, and dyn_cast:
173173 static inline bool classof(const MemIntrinsic *) { return true; }
174174 static inline bool classof(const IntrinsicInst *I) {
191191 public:
192192 /// get* - Return the arguments to the instruction.
193193 ///
194 Value *getValue() const { return const_cast(getOperand(1)); }
195
194 Value *getValue() const { return const_cast(getOperand(2)); }
195
196196 void setValue(Value *Val) {
197197 assert(getValue()->getType() == Val->getType() &&
198 "setValue called with value of wrong type!");
199 setOperand(1, Val);
200 }
201
198 "setSource called with pointer of wrong type!");
199 setOperand(2, Val);
200 }
201
202202 // Methods for support type inquiry through isa, cast, and dyn_cast:
203203 static inline bool classof(const MemSetInst *) { return true; }
204204 static inline bool classof(const IntrinsicInst *I) {
208208 return isa(V) && classof(cast(V));
209209 }
210210 };
211
211
212212 /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
213213 ///
214214 class MemTransferInst : public MemIntrinsic {
215215 public:
216216 /// get* - Return the arguments to the instruction.
217217 ///
218 Value *getRawSource() const { return const_cast(getOperand(1)); }
219
218 Value *getRawSource() const { return const_cast(getOperand(2)); }
219
220220 /// getSource - This is just like getRawSource, but it strips off any cast
221221 /// instructions that feed it, giving the original input. The returned
222222 /// value is guaranteed to be a pointer.
223223 Value *getSource() const { return getRawSource()->stripPointerCasts(); }
224
224
225225 void setSource(Value *Ptr) {
226226 assert(getRawSource()->getType() == Ptr->getType() &&
227227 "setSource called with pointer of wrong type!");
228 setOperand(1, Ptr);
229 }
230
228 setOperand(2, Ptr);
229 }
230
231231 // Methods for support type inquiry through isa, cast, and dyn_cast:
232232 static inline bool classof(const MemTransferInst *) { return true; }
233233 static inline bool classof(const IntrinsicInst *I) {
238238 return isa(V) && classof(cast(V));
239239 }
240240 };
241
242
241
242
243243 /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
244244 ///
245245 class MemCpyInst : public MemTransferInst {
281281 return isa(V) && classof(cast(V));
282282 }
283283 };
284
284
285285 /// MemoryUseIntrinsic - This is the common base class for the memory use
286286 /// marker intrinsics.
287287 ///
254254 private:
255255 /// Returns the operand number of the first argument
256256 unsigned getArgumentOffset() const {
257 if (isCall())
258 return 1; // Skip Function (ATM)
259 else
257260 return 0; // Args are at the front
258261 }
259262
260263 unsigned getArgumentEndOffset() const {
261264 if (isCall())
262 return 1; // Skip Function
265 return 0; // Unchanged (ATM)
263266 else
264267 return 3; // Skip BB, BB, Function
265268 }
266269
267270 IterTy getCallee() const {
268 // FIXME: this is slow, since we do not have the fast versions
269 // of the op_*() functions here. See CallSite::getCallee.
270 //
271 return arg_end();
271 // FIXME: this is slow, since we do not have the fast versions
272 // of the op_*() functions here. See CallSite::getCallee.
273 //
274 if (isCall())
275 return getInstruction()->op_begin(); // Unchanged (ATM)
276 else
277 return getInstruction()->op_end() - 3; // Skip BB, BB, Function
272278 }
273279 };
274280
9393 } else if (const CallInst* CI = extractMallocCall(V)) {
9494 if (!isArrayMalloc(V, &TD))
9595 // The size is the argument to the malloc call.
96 if (const ConstantInt* C = dyn_cast(CI->getOperand(0)))
96 if (const ConstantInt* C = dyn_cast(CI->getOperand(1)))
9797 return (C->getZExtValue() < Size);
9898 return false;
9999 } else if (const Argument *A = dyn_cast(V)) {
317317 case Intrinsic::memcpy:
318318 case Intrinsic::memmove: {
319319 unsigned Len = ~0U;
320 if (ConstantInt *LenCI = dyn_cast(II->getOperand(2)))
320 if (ConstantInt *LenCI = dyn_cast(II->getOperand(3)))
321321 Len = LenCI->getZExtValue();
322 Value *Dest = II->getOperand(0);
323 Value *Src = II->getOperand(1);
322 Value *Dest = II->getOperand(1);
323 Value *Src = II->getOperand(2);
324324 if (isNoAlias(Dest, Len, P, Size)) {
325325 if (isNoAlias(Src, Len, P, Size))
326326 return NoModRef;
331331 case Intrinsic::memset:
332332 // Since memset is 'accesses arguments' only, the AliasAnalysis base class
333333 // will handle it for the variable length case.
334 if (ConstantInt *LenCI = dyn_cast(II->getOperand(2))) {
334 if (ConstantInt *LenCI = dyn_cast(II->getOperand(3))) {
335335 unsigned Len = LenCI->getZExtValue();
336 Value *Dest = II->getOperand(0);
336 Value *Dest = II->getOperand(1);
337337 if (isNoAlias(Dest, Len, P, Size))
338338 return NoModRef;
339339 }
351351 case Intrinsic::atomic_load_umax:
352352 case Intrinsic::atomic_load_umin:
353353 if (TD) {
354 Value *Op1 = II->getOperand(0);
354 Value *Op1 = II->getOperand(1);
355355 unsigned Op1Size = TD->getTypeStoreSize(Op1->getType());
356356 if (isNoAlias(Op1, Op1Size, P, Size))
357357 return NoModRef;
360360 case Intrinsic::lifetime_start:
361361 case Intrinsic::lifetime_end:
362362 case Intrinsic::invariant_start: {
363 unsigned PtrSize = cast(II->getOperand(0))->getZExtValue();
364 if (isNoAlias(II->getOperand(1), PtrSize, P, Size))
363 unsigned PtrSize = cast(II->getOperand(1))->getZExtValue();
364 if (isNoAlias(II->getOperand(2), PtrSize, P, Size))
365365 return NoModRef;
366366 break;
367367 }
368368 case Intrinsic::invariant_end: {
369 unsigned PtrSize = cast(II->getOperand(1))->getZExtValue();
370 if (isNoAlias(II->getOperand(2), PtrSize, P, Size))
369 unsigned PtrSize = cast(II->getOperand(2))->getZExtValue();
370 if (isNoAlias(II->getOperand(3), PtrSize, P, Size))
371371 return NoModRef;
372372 break;
373373 }
771771 case Instruction::ICmp:
772772 case Instruction::FCmp: assert(0 && "Invalid for compares");
773773 case Instruction::Call:
774 if (Function *F = dyn_cast(Ops[NumOps - 1]))
774 if (Function *F = dyn_cast(Ops[0]))
775775 if (canConstantFoldCallTo(F))
776 return ConstantFoldCall(F, Ops, NumOps - 1);
776 return ConstantFoldCall(F, Ops+1, NumOps-1);
777777 return 0;
778778 case Instruction::PtrToInt:
779779 // If the input is a inttoptr, eliminate the pair. This requires knowing
251251 } else if (CallInst *CI = dyn_cast(*UI)) {
252252 // Make sure that this is just the function being called, not that it is
253253 // passing into the function.
254 for (unsigned i = 0, e = CI->getNumOperands() - 1; i != e; ++i)
254 for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i)
255255 if (CI->getOperand(i) == V) return true;
256256 } else if (InvokeInst *II = dyn_cast(*UI)) {
257257 // Make sure that this is just the function being called, not that it is
102102
103103 // If malloc calls' arg can be determined to be a multiple of ElementSize,
104104 // return the multiple. Otherwise, return NULL.
105 Value *MallocArg = CI->getOperand(0);
105 Value *MallocArg = CI->getOperand(1);
106106 Value *Multiple = NULL;
107107 if (ComputeMultiple(MallocArg, ElementSize, Multiple,
108108 LookThroughSExt))
119119 Value *ArraySize = computeArraySize(CI, TD);
120120
121121 if (ArraySize &&
122 ArraySize != ConstantInt::get(CI->getOperand(0)->getType(), 1))
122 ArraySize != ConstantInt::get(CI->getOperand(1)->getType(), 1))
123123 return CI;
124124
125125 // CI is a non-array malloc or we can't figure out that it is an array malloc.
116116 Pointer = V->getOperand(0);
117117 PointerSize = AA->getTypeStoreSize(V->getType());
118118 } else if (isFreeCall(Inst)) {
119 Pointer = Inst->getOperand(0);
119 Pointer = Inst->getOperand(1);
120120 // calls to free() erase the entire structure
121121 PointerSize = ~0ULL;
122122 } else if (isa(Inst) || isa(Inst)) {
196196 // pointer, not on query pointers that are indexed off of them. It'd
197197 // be nice to handle that at some point.
198198 AliasAnalysis::AliasResult R =
199 AA->alias(II->getOperand(2), ~0U, MemPtr, ~0U);
199 AA->alias(II->getOperand(3), ~0U, MemPtr, ~0U);
200200 if (R == AliasAnalysis::MustAlias) {
201 InvariantTag = II->getOperand(0);
201 InvariantTag = II->getOperand(1);
202202 continue;
203203 }
204204
209209 // pointer, not on query pointers that are indexed off of them. It'd
210210 // be nice to handle that at some point.
211211 AliasAnalysis::AliasResult R =
212 AA->alias(II->getOperand(1), ~0U, MemPtr, ~0U);
212 AA->alias(II->getOperand(2), ~0U, MemPtr, ~0U);
213213 if (R == AliasAnalysis::MustAlias)
214214 return MemDepResult::getDef(II);
215215 }
365365 MemSize = AA->getTypeStoreSize(LI->getType());
366366 }
367367 } else if (isFreeCall(QueryInst)) {
368 MemPtr = QueryInst->getOperand(0);
368 MemPtr = QueryInst->getOperand(1);
369369 // calls to free() erase the entire structure, not just a field.
370370 MemSize = ~0UL;
371371 } else if (isa(QueryInst) || isa(QueryInst)) {
377377 case Intrinsic::lifetime_start:
378378 case Intrinsic::lifetime_end:
379379 case Intrinsic::invariant_start:
380 MemPtr = QueryInst->getOperand(1);
381 MemSize = cast(QueryInst->getOperand(0))->getZExtValue();
380 MemPtr = QueryInst->getOperand(2);
381 MemSize = cast(QueryInst->getOperand(1))->getZExtValue();
382382 break;
383383 case Intrinsic::invariant_end:
384 MemPtr = QueryInst->getOperand(2);
385 MemSize = cast(QueryInst->getOperand(1))->getZExtValue();
384 MemPtr = QueryInst->getOperand(3);
385 MemSize = cast(QueryInst->getOperand(2))->getZExtValue();
386386 break;
387387 default:
388388 CallSite QueryCS = CallSite::get(QueryInst);
952952 if (const IntrinsicInst *II = dyn_cast(I))
953953 // sqrt(-0.0) = -0.0, no other negative results are possible.
954954 if (II->getIntrinsicID() == Intrinsic::sqrt)
955 return CannotBeNegativeZero(II->getOperand(0), Depth+1);
955 return CannotBeNegativeZero(II->getOperand(1), Depth+1);
956956
957957 if (const CallInst *CI = dyn_cast(I))
958958 if (const Function *F = CI->getCalledFunction()) {
965965 if (F->getName() == "fabsl") return true;
966966 if (F->getName() == "sqrt" || F->getName() == "sqrtf" ||
967967 F->getName() == "sqrtl")
968 return CannotBeNegativeZero(CI->getOperand(0), Depth+1);
968 return CannotBeNegativeZero(CI->getOperand(1), Depth+1);
969969 }
970970 }
971971
11331133 Vals.push_back(cast(I).isVolatile());
11341134 break;
11351135 case Instruction::Call: {
1136 const CallInst &CI = cast(I);
1137 const PointerType *PTy = cast(CI.getCalledValue()->getType());
1136 const PointerType *PTy = cast(I.getOperand(0)->getType());
11381137 const FunctionType *FTy = cast(PTy->getElementType());
11391138
11401139 Code = bitc::FUNC_CODE_INST_CALL;
11411140
1142 Vals.push_back(VE.getAttributeID(CI.getAttributes()));
1143 Vals.push_back((CI.getCallingConv() << 1) | unsigned(CI.isTailCall()));
1144 PushValueAndType(CI.getCalledValue(), InstID, Vals, VE); // Callee
1141 const CallInst *CI = cast(&I);
1142 Vals.push_back(VE.getAttributeID(CI->getAttributes()));
1143 Vals.push_back((CI->getCallingConv() << 1) | unsigned(CI->isTailCall()));
1144 PushValueAndType(CI->getOperand(0), InstID, Vals, VE); // Callee
11451145
11461146 // Emit value #'s for the fixed parameters.
11471147 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
1148 Vals.push_back(VE.getValueID(I.getOperand(i))); // fixed param.
1148 Vals.push_back(VE.getValueID(I.getOperand(i+1))); // fixed param.
11491149
11501150 // Emit type/value pairs for varargs params.
11511151 if (FTy->isVarArg()) {
1152 for (unsigned i = FTy->getNumParams(), e = I.getNumOperands()-1;
1152 unsigned NumVarargs = I.getNumOperands()-1-FTy->getNumParams();
1153 for (unsigned i = I.getNumOperands()-NumVarargs, e = I.getNumOperands();
11531154 i != e; ++i)
11541155 PushValueAndType(I.getOperand(i), InstID, Vals, VE); // varargs
11551156 }
197197 bool IsCleanUp = (NumOps == 3);
198198
199199 if (!IsCleanUp)
200 if (ConstantInt *CI = dyn_cast(SI->getOperand(2)))
200 if (ConstantInt *CI = dyn_cast(SI->getOperand(3)))
201201 IsCleanUp = (CI->getZExtValue() == 0);
202202
203203 if (IsCleanUp)
236236 if (!Sel || Sel->getParent()->getParent() != F) continue;
237237
238238 // Index of the ".llvm.eh.catch.all.value" variable.
239 unsigned OpIdx = Sel->getNumOperands() - 2;
239 unsigned OpIdx = Sel->getNumOperands() - 1;
240240 GlobalVariable *GV = dyn_cast(Sel->getOperand(OpIdx));
241241 if (GV != EHCatchAllValue) continue;
242242 Sel->setOperand(OpIdx, EHCatchAllValue->getInitializer());
365365 bool IsCleanUp = (NumOps == 3);
366366
367367 if (!IsCleanUp)
368 if (ConstantInt *CI = dyn_cast(II->getOperand(2)))
368 if (ConstantInt *CI = dyn_cast(II->getOperand(3)))
369369 IsCleanUp = (CI->getZExtValue() == 0);
370370
371371 if (IsCleanUp)
389389
390390 // Use the exception object pointer and the personality function
391391 // from the original selector.
392 Args.push_back(II->getOperand(0)); // Exception object pointer.
393 Args.push_back(II->getOperand(1)); // Personality function.
392 Args.push_back(II->getOperand(1)); // Exception object pointer.
393 Args.push_back(II->getOperand(2)); // Personality function.
394394 Args.push_back(EHCatchAllValue->getInitializer()); // Catch-all indicator.
395395
396396 CallInst *NewSelector =
270270 case Intrinsic::gcwrite:
271271 if (LowerWr) {
272272 // Replace a write barrier with a simple store.
273 Value *St = new StoreInst(CI->getOperand(0), CI->getOperand(2), CI);
273 Value *St = new StoreInst(CI->getOperand(1), CI->getOperand(3), CI);
274274 CI->replaceAllUsesWith(St);
275275 CI->eraseFromParent();
276276 }
278278 case Intrinsic::gcread:
279279 if (LowerRd) {
280280 // Replace a read barrier with a simple load.
281 Value *Ld = new LoadInst(CI->getOperand(1), "", CI);
281 Value *Ld = new LoadInst(CI->getOperand(2), "", CI);
282282 Ld->takeName(CI);
283283 CI->replaceAllUsesWith(Ld);
284284 CI->eraseFromParent();
289289 // Initialize the GC root, but do not delete the intrinsic. The
290290 // backend needs the intrinsic to flag the stack slot.
291291 Roots.push_back(cast(
292 CI->getOperand(0)->stripPointerCasts()));
292 CI->getOperand(1)->stripPointerCasts()));
293293 }
294294 break;
295295 default:
307307 static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname,
308308 const char *Dname,
309309 const char *LDname) {
310 switch (CI->getOperand(0)->getType()->getTypeID()) {
310 switch (CI->getOperand(1)->getType()->getTypeID()) {
311311 default: llvm_unreachable("Invalid type in intrinsic");
312312 case Type::FloatTyID:
313 ReplaceCallWith(Fname, CI, CI->op_begin(), CI->op_end() - 1,
313 ReplaceCallWith(Fname, CI, CI->op_begin() + 1, CI->op_end(),
314314 Type::getFloatTy(CI->getContext()));
315315 break;
316316 case Type::DoubleTyID:
317 ReplaceCallWith(Dname, CI, CI->op_begin(), CI->op_end() - 1,
317 ReplaceCallWith(Dname, CI, CI->op_begin() + 1, CI->op_end(),
318318 Type::getDoubleTy(CI->getContext()));
319319 break;
320320 case Type::X86_FP80TyID:
321321 case Type::FP128TyID:
322322 case Type::PPC_FP128TyID:
323 ReplaceCallWith(LDname, CI, CI->op_begin(), CI->op_end() - 1,
324 CI->getOperand(0)->getType());
323 ReplaceCallWith(LDname, CI, CI->op_begin() + 1, CI->op_end(),
324 CI->getOperand(1)->getType());
325325 break;
326326 }
327327 }
346346 // by the lowerinvoke pass. In both cases, the right thing to do is to
347347 // convert the call to an explicit setjmp or longjmp call.
348348 case Intrinsic::setjmp: {
349 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin(), CI->op_end() - 1,
349 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin() + 1, CI->op_end(),
350350 Type::getInt32Ty(Context));
351351 if (!CI->getType()->isVoidTy())
352352 CI->replaceAllUsesWith(V);
358358 break;
359359
360360 case Intrinsic::longjmp: {
361 ReplaceCallWith("longjmp", CI, CI->op_begin(), CI->op_end() - 1,
361 ReplaceCallWith("longjmp", CI, CI->op_begin() + 1, CI->op_end(),
362362 Type::getVoidTy(Context));
363363 break;
364364 }
370370 break;
371371 }
372372 case Intrinsic::ctpop:
373 CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getOperand(0), CI));
373 CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getOperand(1), CI));
374374 break;
375375
376376 case Intrinsic::bswap:
377 CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getOperand(0), CI));
377 CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getOperand(1), CI));
378378 break;
379379
380380 case Intrinsic::ctlz:
381 CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getOperand(0), CI));
381 CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getOperand(1), CI));
382382 break;
383383
384384 case Intrinsic::cttz: {
385385 // cttz(x) -> ctpop(~X & (X-1))
386 Value *Src = CI->getOperand(0);
386 Value *Src = CI->getOperand(1);
387387 Value *NotSrc = Builder.CreateNot(Src);
388388 NotSrc->setName(Src->getName() + ".not");
389389 Value *SrcM1 = ConstantInt::get(Src->getType(), 1);
444444
445445 case Intrinsic::memcpy: {
446446 const IntegerType *IntPtr = TD.getIntPtrType(Context);
447 Value *Size = Builder.CreateIntCast(CI->getOperand(2), IntPtr,
447 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
448448 /* isSigned */ false);
449449 Value *Ops[3];
450 Ops[0] = CI->getOperand(0);
451 Ops[1] = CI->getOperand(1);
450 Ops[0] = CI->getOperand(1);
451 Ops[1] = CI->getOperand(2);
452452 Ops[2] = Size;
453 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(0)->getType());
453 ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType());
454454 break;
455455 }
456456 case Intrinsic::memmove: {
457457 const IntegerType *IntPtr = TD.getIntPtrType(Context);
458 Value *Size = Builder.CreateIntCast(CI->getOperand(2), IntPtr,
458 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
459459 /* isSigned */ false);
460460 Value *Ops[3];
461 Ops[0] = CI->getOperand(0);
462 Ops[1] = CI->getOperand(1);
461 Ops[0] = CI->getOperand(1);
462 Ops[1] = CI->getOperand(2);
463463 Ops[2] = Size;
464 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(0)->getType());
464 ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType());
465465 break;
466466 }
467467 case Intrinsic::memset: {
468468 const IntegerType *IntPtr = TD.getIntPtrType(Context);
469 Value *Size = Builder.CreateIntCast(CI->getOperand(2), IntPtr,
469 Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr,
470470 /* isSigned */ false);
471471 Value *Ops[3];
472 Ops[0] = CI->getOperand(0);
472 Ops[0] = CI->getOperand(1);
473473 // Extend the amount to i32.
474 Ops[1] = Builder.CreateIntCast(CI->getOperand(1), Type::getInt32Ty(Context),
474 Ops[1] = Builder.CreateIntCast(CI->getOperand(2), Type::getInt32Ty(Context),
475475 /* isSigned */ false);
476476 Ops[2] = Size;
477 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(0)->getType());
477 ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType());
478478 break;
479479 }
480480 case Intrinsic::sqrt: {
3030 #include "llvm/Target/TargetData.h"
3131 #include "llvm/Target/TargetFrameInfo.h"
3232 #include "llvm/Target/TargetInstrInfo.h"
33 #include "llvm/Target/TargetIntrinsicInfo.h"
3334 #include "llvm/Target/TargetLowering.h"
3435 #include "llvm/Target/TargetOptions.h"
3536 #include "llvm/Support/Compiler.h"
308309 void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI,
309310 MachineBasicBlock *MBB) {
310311 // Inform the MachineModuleInfo of the personality for this landing pad.
311 const ConstantExpr *CE = cast(I.getOperand(1));
312 const ConstantExpr *CE = cast(I.getOperand(2));
312313 assert(CE->getOpcode() == Instruction::BitCast &&
313314 isa(CE->getOperand(0)) &&
314315 "Personality should be a function");
317318 // Gather all the type infos for this landing pad and pass them along to
318319 // MachineModuleInfo.
319320 std::vector TyInfo;
320 unsigned N = I.getNumOperands() - 1;
321
322 for (unsigned i = N - 1; i > 1; --i) {
321 unsigned N = I.getNumOperands();
322
323 for (unsigned i = N - 1; i > 2; --i) {
323324 if (const ConstantInt *CI = dyn_cast(I.getOperand(i))) {
324325 unsigned FilterLength = CI->getZExtValue();
325326 unsigned FirstCatch = i + FilterLength + !FilterLength;
349350 }
350351 }
351352
352 if (N > 2) {
353 TyInfo.reserve(N - 2);
354 for (unsigned j = 2; j < N; ++j)
353 if (N > 3) {
354 TyInfo.reserve(N - 3);
355 for (unsigned j = 3; j < N; ++j)
355356 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
356357 MMI->addCatchTypeInfo(MBB, TyInfo);
357358 }
27702770 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
27712771
27722772 // Add all operands of the call to the operand list.
2773 for (unsigned i = 0, e = I.getNumOperands()-1; i != e; ++i) {
2773 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
27742774 SDValue Op = getValue(I.getOperand(i));
27752775 assert(TLI.isTypeLegal(Op.getValueType()) &&
27762776 "Intrinsic uses a non-legal type?");
28762876 SDValue Root = getRoot();
28772877 SDValue L =
28782878 DAG.getAtomic(Op, getCurDebugLoc(),
2879 getValue(I.getOperand(1)).getValueType().getSimpleVT(),
2879 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
28802880 Root,
2881 getValue(I.getOperand(0)),
28822881 getValue(I.getOperand(1)),
2883 I.getOperand(0));
2882 getValue(I.getOperand(2)),
2883 I.getOperand(1));
28842884 setValue(&I, L);
28852885 DAG.setRoot(L.getValue(1));
28862886 return 0;
28892889 // implVisitAluOverflow - Lower arithmetic overflow instrinsics.
28902890 const char *
28912891 SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
2892 SDValue Op1 = getValue(I.getOperand(0));
2893 SDValue Op2 = getValue(I.getOperand(1));
2892 SDValue Op1 = getValue(I.getOperand(1));
2893 SDValue Op2 = getValue(I.getOperand(2));
28942894
28952895 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
28962896 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
29042904 SDValue result;
29052905 DebugLoc dl = getCurDebugLoc();
29062906
2907 if (getValue(I.getOperand(0)).getValueType() == MVT::f32 &&
2907 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
29082908 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
2909 SDValue Op = getValue(I.getOperand(0));
2909 SDValue Op = getValue(I.getOperand(1));
29102910
29112911 // Put the exponent in the right bit position for later addition to the
29122912 // final result:
30163016 } else {
30173017 // No special expansion.
30183018 result = DAG.getNode(ISD::FEXP, dl,
3019 getValue(I.getOperand(0)).getValueType(),
3020 getValue(I.getOperand(0)));
3019 getValue(I.getOperand(1)).getValueType(),
3020 getValue(I.getOperand(1)));
30213021 }
30223022
30233023 setValue(&I, result);
30303030 SDValue result;
30313031 DebugLoc dl = getCurDebugLoc();
30323032
3033 if (getValue(I.getOperand(0)).getValueType() == MVT::f32 &&
3033 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
30343034 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3035 SDValue Op = getValue(I.getOperand(0));
3035 SDValue Op = getValue(I.getOperand(1));
30363036 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
30373037
30383038 // Scale the exponent by log(2) [0.69314718f].
31263126 } else {
31273127 // No special expansion.
31283128 result = DAG.getNode(ISD::FLOG, dl,
3129 getValue(I.getOperand(0)).getValueType(),
3130 getValue(I.getOperand(0)));
3129 getValue(I.getOperand(1)).getValueType(),
3130 getValue(I.getOperand(1)));
31313131 }
31323132
31333133 setValue(&I, result);
31403140 SDValue result;
31413141 DebugLoc dl = getCurDebugLoc();
31423142
3143 if (getValue(I.getOperand(0)).getValueType() == MVT::f32 &&
3143 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
31443144 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3145 SDValue Op = getValue(I.getOperand(0));
3145 SDValue Op = getValue(I.getOperand(1));
31463146 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
31473147
31483148 // Get the exponent.
32353235 } else {
32363236 // No special expansion.
32373237 result = DAG.getNode(ISD::FLOG2, dl,
3238 getValue(I.getOperand(0)).getValueType(),
3239 getValue(I.getOperand(0)));
3238 getValue(I.getOperand(1)).getValueType(),
3239 getValue(I.getOperand(1)));
32403240 }
32413241
32423242 setValue(&I, result);
32493249 SDValue result;
32503250 DebugLoc dl = getCurDebugLoc();
32513251
3252 if (getValue(I.getOperand(0)).getValueType() == MVT::f32 &&
3252 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
32533253 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3254 SDValue Op = getValue(I.getOperand(0));
3254 SDValue Op = getValue(I.getOperand(1));
32553255 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
32563256
32573257 // Scale the exponent by log10(2) [0.30102999f].
33373337 } else {
33383338 // No special expansion.
33393339 result = DAG.getNode(ISD::FLOG10, dl,
3340 getValue(I.getOperand(0)).getValueType(),
3341 getValue(I.getOperand(0)));
3340 getValue(I.getOperand(1)).getValueType(),
3341 getValue(I.getOperand(1)));
33423342 }
33433343
33443344 setValue(&I, result);
33513351 SDValue result;
33523352 DebugLoc dl = getCurDebugLoc();
33533353
3354 if (getValue(I.getOperand(0)).getValueType() == MVT::f32 &&
3354 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
33553355 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3356 SDValue Op = getValue(I.getOperand(0));
3356 SDValue Op = getValue(I.getOperand(1));
33573357
33583358 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
33593359
34513451 } else {
34523452 // No special expansion.
34533453 result = DAG.getNode(ISD::FEXP2, dl,
3454 getValue(I.getOperand(0)).getValueType(),
3455 getValue(I.getOperand(0)));
3454 getValue(I.getOperand(1)).getValueType(),
3455 getValue(I.getOperand(1)));
34563456 }
34573457
34583458 setValue(&I, result);
34633463 void
34643464 SelectionDAGBuilder::visitPow(const CallInst &I) {
34653465 SDValue result;
3466 const Value *Val = I.getOperand(0);
3466 const Value *Val = I.getOperand(1);
34673467 DebugLoc dl = getCurDebugLoc();
34683468 bool IsExp10 = false;
34693469
34703470 if (getValue(Val).getValueType() == MVT::f32 &&
3471 getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
3471 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
34723472 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
34733473 if (Constant *C = const_cast(dyn_cast(Val))) {
34743474 if (ConstantFP *CFP = dyn_cast(C)) {
34793479 }
34803480
34813481 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3482 SDValue Op = getValue(I.getOperand(1));
3482 SDValue Op = getValue(I.getOperand(2));
34833483
34843484 // Put the exponent in the right bit position for later addition to the
34853485 // final result:
35843584 } else {
35853585 // No special expansion.
35863586 result = DAG.getNode(ISD::FPOW, dl,
3587 getValue(I.getOperand(0)).getValueType(),
3588 getValue(I.getOperand(0)),
3589 getValue(I.getOperand(1)));
3587 getValue(I.getOperand(1)).getValueType(),
3588 getValue(I.getOperand(1)),
3589 getValue(I.getOperand(2)));
35903590 }
35913591
35923592 setValue(&I, result);
36643664 case Intrinsic::vacopy: visitVACopy(I); return 0;
36653665 case Intrinsic::returnaddress:
36663666 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
3667 getValue(I.getOperand(0))));
3667 getValue(I.getOperand(1))));
36683668 return 0;
36693669 case Intrinsic::frameaddress:
36703670 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
3671 getValue(I.getOperand(0))));
3671 getValue(I.getOperand(1))));
36723672 return 0;
36733673 case Intrinsic::setjmp:
36743674 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
36773677 case Intrinsic::memcpy: {
36783678 // Assert for address < 256 since we support only user defined address
36793679 // spaces.
3680 assert(cast(I.getOperand(0)->getType())->getAddressSpace()
3680 assert(cast(I.getOperand(1)->getType())->getAddressSpace()
36813681 < 256 &&
3682 cast(I.getOperand(1)->getType())->getAddressSpace()
3682 cast(I.getOperand(2)->getType())->getAddressSpace()
36833683 < 256 &&
36843684 "Unknown address space");
3685 SDValue Op1 = getValue(I.getOperand(0));
3686 SDValue Op2 = getValue(I.getOperand(1));
3687 SDValue Op3 = getValue(I.getOperand(2));
3688 unsigned Align = cast(I.getOperand(3))->getZExtValue();
3689 bool isVol = cast(I.getOperand(4))->getZExtValue();
3685 SDValue Op1 = getValue(I.getOperand(1));
3686 SDValue Op2 = getValue(I.getOperand(2));
3687 SDValue Op3 = getValue(I.getOperand(3));
3688 unsigned Align = cast(I.getOperand(4))->getZExtValue();
3689 bool isVol = cast(I.getOperand(5))->getZExtValue();
36903690 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
3691 I.getOperand(0), 0, I.getOperand(1), 0));
3691 I.getOperand(1), 0, I.getOperand(2), 0));
36923692 return 0;
36933693 }
36943694 case Intrinsic::memset: {
36953695 // Assert for address < 256 since we support only user defined address
36963696 // spaces.
3697 assert(cast(I.getOperand(0)->getType())->getAddressSpace()
3697 assert(cast(I.getOperand(1)->getType())->getAddressSpace()
36983698 < 256 &&
36993699 "Unknown address space");
3700 SDValue Op1 = getValue(I.getOperand(0));
3701 SDValue Op2 = getValue(I.getOperand(1));
3702 SDValue Op3 = getValue(I.getOperand(2));
3703 unsigned Align = cast(I.getOperand(3))->getZExtValue();
3704 bool isVol = cast(I.getOperand(4))->getZExtValue();
3700 SDValue Op1 = getValue(I.getOperand(1));
3701 SDValue Op2 = getValue(I.getOperand(2));
3702 SDValue Op3 = getValue(I.getOperand(3));
3703 unsigned Align = cast(I.getOperand(4))->getZExtValue();
3704 bool isVol = cast(I.getOperand(5))->getZExtValue();
37053705 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
3706 I.getOperand(0), 0));
3706 I.getOperand(1), 0));
37073707 return 0;
37083708 }
37093709 case Intrinsic::memmove: {
37103710 // Assert for address < 256 since we support only user defined address
37113711 // spaces.
3712 assert(cast(I.getOperand(0)->getType())->getAddressSpace()
3712 assert(cast(I.getOperand(1)->getType())->getAddressSpace()
37133713 < 256 &&
3714 cast(I.getOperand(1)->getType())->getAddressSpace()
3714 cast(I.getOperand(2)->getType())->getAddressSpace()
37153715 < 256 &&
37163716 "Unknown address space");
3717 SDValue Op1 = getValue(I.getOperand(0));
3718 SDValue Op2 = getValue(I.getOperand(1));
3719 SDValue Op3 = getValue(I.getOperand(2));
3720 unsigned Align = cast(I.getOperand(3))->getZExtValue();
3721 bool isVol = cast(I.getOperand(4))->getZExtValue();
3717 SDValue Op1 = getValue(I.getOperand(1));
3718 SDValue Op2 = getValue(I.getOperand(2));
3719 SDValue Op3 = getValue(I.getOperand(3));
3720 unsigned Align = cast(I.getOperand(4))->getZExtValue();
3721 bool isVol = cast(I.getOperand(5))->getZExtValue();
37223722
37233723 // If the source and destination are known to not be aliases, we can
37243724 // lower memmove as memcpy.
37253725 uint64_t Size = -1ULL;
37263726 if (ConstantSDNode *C = dyn_cast(Op3))
37273727 Size = C->getZExtValue();
3728 if (AA->alias(I.getOperand(0), Size, I.getOperand(1), Size) ==
3728 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
37293729 AliasAnalysis::NoAlias) {
37303730 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
3731 false, I.getOperand(0), 0, I.getOperand(1), 0));
3731 false, I.getOperand(1), 0, I.getOperand(2), 0));
37323732 return 0;
37333733 }
37343734
37353735 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
3736 I.getOperand(0), 0, I.getOperand(1), 0));
3736 I.getOperand(1), 0, I.getOperand(2), 0));
37373737 return 0;
37383738 }
37393739 case Intrinsic::dbg_declare: {
38453845 // Insert the EHSELECTION instruction.
38463846 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
38473847 SDValue Ops[2];
3848 Ops[0] = getValue(I.getOperand(0));
3848 Ops[0] = getValue(I.getOperand(1));
38493849 Ops[1] = getRoot();
38503850 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
38513851 DAG.setRoot(Op.getValue(1));
38553855
38563856 case Intrinsic::eh_typeid_for: {
38573857 // Find the type id for the given typeinfo.
3858 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(0));
3858 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
38593859 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
38603860 Res = DAG.getConstant(TypeID, MVT::i32);
38613861 setValue(&I, Res);
38683868 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
38693869 MVT::Other,
38703870 getControlRoot(),
3871 getValue(I.getOperand(0)),
3872 getValue(I.getOperand(1))));
3871 getValue(I.getOperand(1)),
3872 getValue(I.getOperand(2))));
38733873 return 0;
38743874 case Intrinsic::eh_unwind_init:
38753875 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
38763876 return 0;
38773877 case Intrinsic::eh_dwarf_cfa: {
3878 EVT VT = getValue(I.getOperand(0)).getValueType();
3879 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(0)), dl,
3878 EVT VT = getValue(I.getOperand(1)).getValueType();
3879 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
38803880 TLI.getPointerTy());
38813881 SDValue Offset = DAG.getNode(ISD::ADD, dl,
38823882 TLI.getPointerTy(),
38923892 }
38933893 case Intrinsic::eh_sjlj_callsite: {
38943894 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3895 ConstantInt *CI = dyn_cast(I.getOperand(0));
3895 ConstantInt *CI = dyn_cast(I.getOperand(1));
38963896 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
38973897 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
38983898
39223922 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
39233923 }
39243924 EVT DestVT = TLI.getValueType(I.getType());
3925 const Value *Op1 = I.getOperand(0);
3925 const Value *Op1 = I.getOperand(1);
39263926 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
39273927 DAG.getValueType(DestVT),
39283928 DAG.getValueType(getValue(Op1).getValueType()),
3929 getValue(I.getOperand(1)),
39303929 getValue(I.getOperand(2)),
3930 getValue(I.getOperand(3)),
39313931 Code);
39323932 setValue(&I, Res);
39333933 return 0;
39343934 }
39353935 case Intrinsic::sqrt:
39363936 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
3937 getValue(I.getOperand(0)).getValueType(),
3938 getValue(I.getOperand(0))));
3937 getValue(I.getOperand(1)).getValueType(),
3938 getValue(I.getOperand(1))));
39393939 return 0;
39403940 case Intrinsic::powi:
3941 setValue(&I, ExpandPowI(dl, getValue(I.getOperand(0)),
3942 getValue(I.getOperand(1)), DAG));
3941 setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)),
3942 getValue(I.getOperand(2)), DAG));
39433943 return 0;
39443944 case Intrinsic::sin:
39453945 setValue(&I, DAG.getNode(ISD::FSIN, dl,
3946 getValue(I.getOperand(0)).getValueType(),
3947 getValue(I.getOperand(0))));
3946 getValue(I.getOperand(1)).getValueType(),
3947 getValue(I.getOperand(1))));
39483948 return 0;
39493949 case Intrinsic::cos:
39503950 setValue(&I, DAG.getNode(ISD::FCOS, dl,
3951 getValue(I.getOperand(0)).getValueType(),
3952 getValue(I.getOperand(0))));
3951 getValue(I.getOperand(1)).getValueType(),
3952 getValue(I.getOperand(1))));
39533953 return 0;
39543954 case Intrinsic::log:
39553955 visitLog(I);
39713971 return 0;
39723972 case Intrinsic::convert_to_fp16:
39733973 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
3974 MVT::i16, getValue(I.getOperand(0))));
3974 MVT::i16, getValue(I.getOperand(1))));
39753975 return 0;
39763976 case Intrinsic::convert_from_fp16:
39773977 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
3978 MVT::f32, getValue(I.getOperand(0))));
3978 MVT::f32, getValue(I.getOperand(1))));
39793979 return 0;
39803980 case Intrinsic::pcmarker: {
3981 SDValue Tmp = getValue(I.getOperand(0));
3981 SDValue Tmp = getValue(I.getOperand(1));
39823982 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
39833983 return 0;
39843984 }
39933993 }
39943994 case Intrinsic::bswap:
39953995 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
3996 getValue(I.getOperand(0)).getValueType(),
3997 getValue(I.getOperand(0))));
3996 getValue(I.getOperand(1)).getValueType(),
3997 getValue(I.getOperand(1))));
39983998 return 0;
39993999 case Intrinsic::cttz: {
4000 SDValue Arg = getValue(I.getOperand(0));
4000 SDValue Arg = getValue(I.getOperand(1));
40014001 EVT Ty = Arg.getValueType();
40024002 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
40034003 return 0;
40044004 }
40054005 case Intrinsic::ctlz: {
4006 SDValue Arg = getValue(I.getOperand(0));
4006 SDValue Arg = getValue(I.getOperand(1));
40074007 EVT Ty = Arg.getValueType();
40084008 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
40094009 return 0;
40104010 }
40114011 case Intrinsic::ctpop: {
4012 SDValue Arg = getValue(I.getOperand(0));
4012 SDValue Arg = getValue(I.getOperand(1));
40134013 EVT Ty = Arg.getValueType();
40144014 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
40154015 return 0;
40234023 return 0;
40244024 }
40254025 case Intrinsic::stackrestore: {
4026 Res = getValue(I.getOperand(0));
4026 Res = getValue(I.getOperand(1));
40274027 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
40284028 return 0;
40294029 }
40334033 MachineFrameInfo *MFI = MF.getFrameInfo();
40344034 EVT PtrTy = TLI.getPointerTy();
40354035
4036 SDValue Src = getValue(I.getOperand(0)); // The guard's value.
4037 AllocaInst *Slot = cast(I.getOperand(1));
4036 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4037 AllocaInst *Slot = cast(I.getOperand(2));
40384038
40394039 int FI = FuncInfo.StaticAllocaMap[Slot];
40404040 MFI->setStackProtectorIndex(FI);
40514051 }
40524052 case Intrinsic::objectsize: {
40534053 // If we don't know by now, we're never going to know.
4054 ConstantInt *CI = dyn_cast(I.getOperand(1));
4054 ConstantInt *CI = dyn_cast(I.getOperand(2));
40554055
40564056 assert(CI && "Non-constant type in __builtin_object_size?");
40574057
4058 SDValue Arg = getValue(I.getCalledValue());
4058 SDValue Arg = getValue(I.getOperand(0));
40594059 EVT Ty = Arg.getValueType();
40604060
40614061 if (CI->getZExtValue() == 0)
40714071 return 0;
40724072
40734073 case Intrinsic::init_trampoline: {
4074 const Function *F = cast(I.getOperand(1)->stripPointerCasts());
4074 const Function *F = cast(I.getOperand(2)->stripPointerCasts());
40754075
40764076 SDValue Ops[6];
40774077 Ops[0] = getRoot();
4078 Ops[1] = getValue(I.getOperand(0));
4079 Ops[2] = getValue(I.getOperand(1));
4080 Ops[3] = getValue(I.getOperand(2));
4081 Ops[4] = DAG.getSrcValue(I.getOperand(0));
4078 Ops[1] = getValue(I.getOperand(1));
4079 Ops[2] = getValue(I.getOperand(2));
4080 Ops[3] = getValue(I.getOperand(3));
4081 Ops[4] = DAG.getSrcValue(I.getOperand(1));
40824082 Ops[5] = DAG.getSrcValue(F);
40834083
40844084 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
40914091 }
40924092 case Intrinsic::gcroot:
40934093 if (GFI) {
4094 const Value *Alloca = I.getOperand(0);
4095 const Constant *TypeMap = cast(I.getOperand(1));
4094 const Value *Alloca = I.getOperand(1);
4095 const Constant *TypeMap = cast(I.getOperand(2));
40964096
40974097 FrameIndexSDNode *FI = cast(getValue(Alloca).getNode());
40984098 GFI->addStackRoot(FI->getIndex(), TypeMap);
41244124 case Intrinsic::prefetch: {
41254125 SDValue Ops[4];
41264126 Ops[0] = getRoot();
4127 Ops[1] = getValue(I.getOperand(0));
4128 Ops[2] = getValue(I.getOperand(1));
4129 Ops[3] = getValue(I.getOperand(2));
4127 Ops[1] = getValue(I.getOperand(1));
4128 Ops[2] = getValue(I.getOperand(2));
4129 Ops[3] = getValue(I.getOperand(3));
41304130 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
41314131 return 0;
41324132 }
41354135 SDValue Ops[6];
41364136 Ops[0] = getRoot();
41374137 for (int x = 1; x < 6; ++x)
4138 Ops[x] = getValue(I.getOperand(x - 1));
4138 Ops[x] = getValue(I.getOperand(x));
41394139
41404140 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
41414141 return 0;
41444144 SDValue Root = getRoot();
41454145 SDValue L =
41464146 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
4147 getValue(I.getOperand(1)).getValueType().getSimpleVT(),
4147 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
41484148 Root,
4149 getValue(I.getOperand(0)),
41504149 getValue(I.getOperand(1)),
41514150 getValue(I.getOperand(2)),
4152 I.getOperand(0));
4151 getValue(I.getOperand(3)),
4152 I.getOperand(1));
41534153 setValue(&I, L);
41544154 DAG.setRoot(L.getValue(1));
41554155 return 0;
45194519 if (I.getNumOperands() != 4)
45204520 return false;
45214521
4522 const Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
4522 const Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
45234523 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
4524 !I.getOperand(2)->getType()->isIntegerTy() ||
4524 !I.getOperand(3)->getType()->isIntegerTy() ||
45254525 !I.getType()->isIntegerTy())
45264526 return false;
45274527
4528 const ConstantInt *Size = dyn_cast(I.getOperand(2));
4528 const ConstantInt *Size = dyn_cast(I.getOperand(3));
45294529
45304530 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
45314531 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
46164616 StringRef Name = F->getName();
46174617 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
46184618 if (I.getNumOperands() == 3 && // Basic sanity checks.
4619 I.getOperand(0)->getType()->isFloatingPointTy() &&
4620 I.getType() == I.getOperand(0)->getType() &&
4621 I.getType() == I.getOperand(1)->getType()) {
4622 SDValue LHS = getValue(I.getOperand(0));
4623 SDValue RHS = getValue(I.getOperand(1));
4619 I.getOperand(1)->getType()->isFloatingPointTy() &&
4620 I.getType() == I.getOperand(1)->getType() &&
4621 I.getType() == I.getOperand(2)->getType()) {
4622 SDValue LHS = getValue(I.getOperand(1));
4623 SDValue RHS = getValue(I.getOperand(2));
46244624 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
46254625 LHS.getValueType(), LHS, RHS));
46264626 return;
46274627 }
46284628 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
46294629 if (I.getNumOperands() == 2 && // Basic sanity checks.
4630 I.getOperand(0)->getType()->isFloatingPointTy() &&
4631 I.getType() == I.getOperand(0)->getType()) {
4632 SDValue Tmp = getValue(I.getOperand(0));
4630 I.getOperand(1)->getType()->isFloatingPointTy() &&
4631 I.getType() == I.getOperand(1)->getType()) {
4632 SDValue Tmp = getValue(I.getOperand(1));
46334633 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
46344634 Tmp.getValueType(), Tmp));
46354635 return;
46364636 }
46374637 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
46384638 if (I.getNumOperands() == 2 && // Basic sanity checks.
4639 I.getOperand(0)->getType()->isFloatingPointTy() &&
4640 I.getType() == I.getOperand(0)->getType() &&
4639 I.getOperand(1)->getType()->isFloatingPointTy() &&
4640 I.getType() == I.getOperand(1)->getType() &&
46414641 I.onlyReadsMemory()) {
4642 SDValue Tmp = getValue(I.getOperand(0));
4642 SDValue Tmp = getValue(I.getOperand(1));
46434643 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
46444644 Tmp.getValueType(), Tmp));
46454645 return;
46464646 }
46474647 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
46484648 if (I.getNumOperands() == 2 && // Basic sanity checks.
4649 I.getOperand(0)->getType()->isFloatingPointTy() &&
4650 I.getType() == I.getOperand(0)->getType() &&
4649 I.getOperand(1)->getType()->isFloatingPointTy() &&
4650 I.getType() == I.getOperand(1)->getType() &&
46514651 I.onlyReadsMemory()) {
4652 SDValue Tmp = getValue(I.getOperand(0));
4652 SDValue Tmp = getValue(I.getOperand(1));
46534653 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
46544654 Tmp.getValueType(), Tmp));
46554655 return;
46564656 }
46574657 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
46584658 if (I.getNumOperands() == 2 && // Basic sanity checks.
4659 I.getOperand(0)->getType()->isFloatingPointTy() &&
4660 I.getType() == I.getOperand(0)->getType() &&
4659 I.getOperand(1)->getType()->isFloatingPointTy() &&
4660 I.getType() == I.getOperand(1)->getType() &&
46614661 I.onlyReadsMemory()) {
4662 SDValue Tmp = getValue(I.getOperand(0));
4662 SDValue Tmp = getValue(I.getOperand(1));
46634663 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
46644664 Tmp.getValueType(), Tmp));
46654665 return;
46694669 return;
46704670 }
46714671 }
4672 } else if (isa(I.getCalledValue())) {
4672 } else if (isa(I.getOperand(0))) {
46734673 visitInlineAsm(&I);
46744674 return;
46754675 }
46764676
46774677 SDValue Callee;
46784678 if (!RenameFn)
4679 Callee = getValue(I.getCalledValue());
4679 Callee = getValue(I.getOperand(0));
46804680 else
46814681 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
46824682
56085608 void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
56095609 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
56105610 MVT::Other, getRoot(),
5611 getValue(I.getOperand(0)),
5612 DAG.getSrcValue(I.getOperand(0))));
5611 getValue(I.getOperand(1)),
5612 DAG.getSrcValue(I.getOperand(1))));
56135613 }
56145614
56155615 void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
56235623 void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
56245624 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
56255625 MVT::Other, getRoot(),
5626 getValue(I.getOperand(0)),
5627 DAG.getSrcValue(I.getOperand(0))));
5626 getValue(I.getOperand(1)),
5627 DAG.getSrcValue(I.getOperand(1))));
56285628 }
56295629
56305630 void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
56315631 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
56325632 MVT::Other, getRoot(),
5633 getValue(I.getOperand(0)),
56345633 getValue(I.getOperand(1)),
5635 DAG.getSrcValue(I.getOperand(0)),
5636 DAG.getSrcValue(I.getOperand(1))));
5634 getValue(I.getOperand(2)),
5635 DAG.getSrcValue(I.getOperand(1)),
5636 DAG.getSrcValue(I.getOperand(2))));
56375637 }
56385638
56395639 /// TargetLowering::LowerCallTo - This is the default LowerCallTo
157157
158158 // Create a new invoke instruction.
159159 Args.clear();
160 Args.append(CI->op_begin(), CI->op_end() - 1);
161
162 InvokeInst *II = InvokeInst::Create(CI->getCalledValue(),
160 Args.append(CI->op_begin() + 1, CI->op_end());
161
162 InvokeInst *II = InvokeInst::Create(CI->getOperand(0),
163163 NewBB, CleanupBB,
164164 Args.begin(), Args.end(),
165165 CI->getName(), CallBB);
193193 unsigned NumMeta = 0;
194194 SmallVector Metadata;
195195 for (unsigned I = 0; I != Roots.size(); ++I) {
196 Constant *C = cast(Roots[I].first->getOperand(1));
196 Constant *C = cast(Roots[I].first->getOperand(2));
197197 if (!C->isNullValue())
198198 NumMeta = I + 1;
199199 Metadata.push_back(ConstantExpr::getBitCast(C, VoidPtr));
321321
322322 assert(Roots.empty() && "Not cleaned up?");
323323
324 SmallVector AllocaInst*>,16> MetaRoots;
324 SmallVectorAllocaInst*>,16> MetaRoots;
325325
326326 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
327327 for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;)
328328 if (IntrinsicInst *CI = dyn_cast(II++))
329329 if (Function *F = CI->getCalledFunction())
330330 if (F->getIntrinsicID() == Intrinsic::gcroot) {
331 std::pair Pair = std::make_pair(
332 CI, cast(CI->getOperand(0)->stripPointerCasts()));
333 if (IsNullValue(CI->getOperand(1)))
331 std::pair Pair = std::make_pair(
332 CI, cast(CI->getOperand(1)->stripPointerCasts()));
333 if (IsNullValue(CI->getOperand(2)))
334334 Roots.push_back(Pair);
335335 else
336336 MetaRoots.push_back(Pair);
304304 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
305305 if (CallInst *CI = dyn_cast(I)) {
306306 if (CI->getCalledFunction() == SelectorFn) {
307 if (!PersonalityFn) PersonalityFn = CI->getOperand(1);
307 if (!PersonalityFn) PersonalityFn = CI->getOperand(2);
308308 EH_Selectors.push_back(CI);
309309 } else if (CI->getCalledFunction() == ExceptionFn) {
310310 EH_Exceptions.push_back(CI);
28852885 bool hasByVal = I.hasByValArgument();
28862886 bool isStructRet = I.hasStructRetAttr();
28872887 if (isStructRet) {
2888 writeOperandDeref(I.getOperand(0));
2888 writeOperandDeref(I.getOperand(1));
28892889 Out << " = ";
28902890 }
28912891
29412941
29422942 unsigned NumDeclaredParams = FTy->getNumParams();
29432943
2944 CallInst::op_iterator AI = I.op_begin(), AE = I.op_end() - 1;
2944 CallSite::arg_iterator AI = I.op_begin()+1, AE = I.op_end();
29452945 unsigned ArgNo = 0;
29462946 if (isStructRet) { // Skip struct return argument.
29472947 ++AI;
29952995 Out << "0; ";
29962996
29972997 Out << "va_start(*(va_list*)";
2998 writeOperand(I.getOperand(0));
2998 writeOperand(I.getOperand(1));
29992999 Out << ", ";
30003000 // Output the last argument to the enclosing function.
30013001 if (I.getParent()->getParent()->arg_empty())
30053005 Out << ')';
30063006 return true;
30073007 case Intrinsic::vaend:
3008 if (!isa(I.getOperand(0))) {
3008 if (!isa(I.getOperand(1))) {
30093009 Out << "0; va_end(*(va_list*)";
3010 writeOperand(I.getOperand(0));
3010 writeOperand(I.getOperand(1));
30113011 Out << ')';
30123012 } else {
30133013 Out << "va_end(*(va_list*)0)";
30163016 case Intrinsic::vacopy:
30173017 Out << "0; ";
30183018 Out << "va_copy(*(va_list*)";
3019 writeOperand(I.getOperand(0));
3019 writeOperand(I.getOperand(1));
30203020 Out << ", *(va_list*)";
3021 writeOperand(I.getOperand(1));
3021 writeOperand(I.getOperand(2));
30223022 Out << ')';
30233023 return true;
30243024 case Intrinsic::returnaddress:
30253025 Out << "__builtin_return_address(";
3026 writeOperand(I.getOperand(0));
3026 writeOperand(I.getOperand(1));
30273027 Out << ')';
30283028 return true;
30293029 case Intrinsic::frameaddress:
30303030 Out << "__builtin_frame_address(";
3031 writeOperand(I.getOperand(0));
3031 writeOperand(I.getOperand(1));
30323032 Out << ')';
30333033 return true;
30343034 case Intrinsic::powi:
30353035 Out << "__builtin_powi(";
3036 writeOperand(I.getOperand(0));
3036 writeOperand(I.getOperand(1));
30373037 Out << ", ";
3038 writeOperand(I.getOperand(1));
3038 writeOperand(I.getOperand(2));
30393039 Out << ')';
30403040 return true;
30413041 case Intrinsic::setjmp:
30423042 Out << "setjmp(*(jmp_buf*)";
3043 writeOperand(I.getOperand(0));
3043 writeOperand(I.getOperand(1));
30443044 Out << ')';
30453045 return true;
30463046 case Intrinsic::longjmp:
30473047 Out << "longjmp(*(jmp_buf*)";
3048 writeOperand(I.getOperand(0));
3048 writeOperand(I.getOperand(1));
30493049 Out << ", ";
3050 writeOperand(I.getOperand(1));
3050 writeOperand(I.getOperand(2));
30513051 Out << ')';
30523052 return true;
30533053 case Intrinsic::prefetch:
30543054 Out << "LLVM_PREFETCH((const void *)";
3055 writeOperand(I.getOperand(0));
3056 Out << ", ";
30573055 writeOperand(I.getOperand(1));
30583056 Out << ", ";
30593057 writeOperand(I.getOperand(2));
3058 Out << ", ";
3059 writeOperand(I.getOperand(3));
30603060 Out << ")";
30613061 return true;
30623062 case Intrinsic::stacksave:
30733073 printType(Out, I.getType());
30743074 Out << ')';
30753075 // Multiple GCC builtins multiplex onto this intrinsic.
3076 switch (cast(I.getOperand(2))->getZExtValue()) {
3076 switch (cast(I.getOperand(3))->getZExtValue()) {
30773077 default: llvm_unreachable("Invalid llvm.x86.sse.cmp!");
30783078 case 0: Out << "__builtin_ia32_cmpeq"; break;
30793079 case 1: Out << "__builtin_ia32_cmplt"; break;
30943094 Out << 'd';
30953095
30963096 Out << "(";
3097 writeOperand(I.getOperand(0));
3097 writeOperand(I.getOperand(1));
30983098 Out << ", ";
3099 writeOperand(I.getOperand(1));
3099 writeOperand(I.getOperand(2));
31003100 Out << ")";
31013101 return true;
31023102 case Intrinsic::ppc_altivec_lvsl:
31043104 printType(Out, I.getType());
31053105 Out << ')';
31063106 Out << "__builtin_altivec_lvsl(0, (void*)";
3107 writeOperand(I.getOperand(0));
3107 writeOperand(I.getOperand(1));
31083108 Out << ")";
31093109 return true;
31103110 }
32173217 DestVal = ResultVals[ValueCount].first;
32183218 DestValNo = ResultVals[ValueCount].second;
32193219 } else
3220 DestVal = CI.getOperand(ValueCount-ResultVals.size());
3220 DestVal = CI.getOperand(ValueCount-ResultVals.size()+1);
32213221
32223222 if (I->isEarlyClobber)
32233223 C = "&"+C;
32513251 }
32523252
32533253 assert(ValueCount >= ResultVals.size() && "Input can't refer to result");
3254 Value *SrcVal = CI.getOperand(ValueCount-ResultVals.size());
3254 Value *SrcVal = CI.getOperand(ValueCount-ResultVals.size()+1);
32553255
32563256 Out << "\"" << C << "\"(";
32573257 if (!I->isIndirect)
10811081
10821082 // Before we emit this instruction, we need to take care of generating any
10831083 // forward references. So, we get the names of all the operands in advance
1084 const unsigned Ops(I->getNumOperands());
1085 std::string* opNames = new std::string[Ops];
1086 for (unsigned i = 0; i < Ops; i++) {
1084 std::string* opNames = new std::string[I->getNumOperands()];
1085 for (unsigned i = 0; i < I->getNumOperands(); i++) {
10871086 opNames[i] = getOpName(I->getOperand(i));
10881087 }
10891088
11441143 const InvokeInst* inv = cast(I);
11451144 Out << "std::vector " << iName << "_params;";
11461145 nl(Out);
1147 for (unsigned i = 0; i < inv->getNumOperands() - 3; ++i) {
1146 for (unsigned i = 3; i < inv->getNumOperands(); ++i) {
11481147 Out << iName << "_params.push_back("
11491148 << opNames[i] << ");";
11501149 nl(Out);
11511150 }
11521151 Out << "InvokeInst *" << iName << " = InvokeInst::Create("
1153 << opNames[Ops - 3] << ", "
1154 << opNames[Ops - 2] << ", "
1155 << opNames[Ops - 1] << ", "
1152 << opNames[0] << ", "
1153 << opNames[1] << ", "
1154 << opNames[2] << ", "
11561155 << iName << "_params.begin(), " << iName << "_params.end(), \"";
11571156 printEscapedString(inv->getName());
11581157 Out << "\", " << bbname << ");";
13881387 if (call->getNumOperands() > 2) {
13891388 Out << "std::vector " << iName << "_params;";
13901389 nl(Out);
1391 for (unsigned i = 0; i < call->getNumOperands() - 1; ++i) {
1390 for (unsigned i = 1; i < call->getNumOperands(); ++i) {
13921391 Out << iName << "_params.push_back(" << opNames[i] << ");";
13931392 nl(Out);
13941393 }
13951394 Out << "CallInst* " << iName << " = CallInst::Create("
1396 << opNames[Ops - 1] << ", " << iName << "_params.begin(), "
1395 << opNames[0] << ", " << iName << "_params.begin(), "
13971396 << iName << "_params.end(), \"";
13981397 } else if (call->getNumOperands() == 2) {
13991398 Out << "CallInst* " << iName << " = CallInst::Create("
1400 << opNames[Ops - 1] << ", " << opNames[0] << ", \"";
1399 << opNames[0] << ", " << opNames[1] << ", \"";
14011400 } else {
1402 Out << "CallInst* " << iName << " = CallInst::Create(" << opNames[Ops - 1]
1401 Out << "CallInst* " << iName << " = CallInst::Create(" << opNames[0]
14031402 << ", \"";
14041403 }
14051404 printEscapedString(call->getName());
11691169 // Emit code inline code to store the stack guard onto the stack.
11701170 EVT PtrTy = TLI.getPointerTy();
11711171
1172 const Value *Op1 = I.getOperand(0); // The guard's value.
1173 const AllocaInst *Slot = cast(I.getOperand(1));
1172 const Value *Op1 = I.getOperand(1); // The guard's value.
1173 const AllocaInst *Slot = cast(I.getOperand(2));
11741174
11751175 // Grab the frame index.
11761176 X86AddressMode AM;
11811181 return true;
11821182 }
11831183 case Intrinsic::objectsize: {
1184 ConstantInt *CI = dyn_cast(I.getOperand(1));
1184 ConstantInt *CI = dyn_cast(I.getOperand(2));
11851185 const Type *Ty = I.getCalledFunction()->getReturnType();
11861186
11871187 assert(CI && "Non-constant type in Intrinsic::objectsize?");
12361236 if (!isTypeLegal(RetTy, VT))
12371237 return false;
12381238
1239 const Value *Op1 = I.getOperand(0);
1240 const Value *Op2 = I.getOperand(1);
1239 const Value *Op1 = I.getOperand(1);
1240 const Value *Op2 = I.getOperand(2);
12411241 unsigned Reg1 = getRegForValue(Op1);
12421242 unsigned Reg2 = getRegForValue(Op2);
12431243
12801280
12811281 bool X86FastISel::X86SelectCall(const Instruction *I) {
12821282 const CallInst *CI = cast(I);
1283 const Value *Callee = CI->getCalledValue();
1283 const Value *Callee = I->getOperand(0);
12841284
12851285 // Can't handle inline asm yet.
12861286 if (isa(Callee))
99589958
99599959 // Verify this is a simple bswap.
99609960 if (CI->getNumOperands() != 2 ||
9961 CI->getType() != CI->getOperand(0)->getType() ||
9961 CI->getType() != CI->getOperand(1)->getType() ||
99629962 !CI->getType()->isIntegerTy())
99639963 return false;
99649964
99719971 Module *M = CI->getParent()->getParent()->getParent();
99729972 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
99739973
9974 Value *Op = CI->getOperand(0);
9974 Value *Op = CI->getOperand(1);
99759975 Op = CallInst::Create(Int, Op, CI->getName(), CI);
99769976
99779977 CI->replaceAllUsesWith(Op);
221221 GS.HasPHIUser = true;
222222 } else if (isa(I)) {
223223 } else if (isa(I)) {
224 if (I->getOperand(0) == V)
224 if (I->getOperand(1) == V)
225225 GS.StoredType = GlobalStatus::isStored;
226 if (I->getOperand(1) == V)
226 if (I->getOperand(2) == V)
227227 GS.isLoaded = true;
228228 } else if (isa(I)) {
229 assert(I->getOperand(0) == V && "Memset only takes one pointer!");
229 assert(I->getOperand(1) == V && "Memset only takes one pointer!");
230230 GS.StoredType = GlobalStatus::isStored;
231231 } else {
232232 return true; // Any other non-load instruction might take address!
13221322 // if (F2) { free(F2); F2 = 0; }
13231323 // }
13241324 // The malloc can also fail if its argument is too large.
1325 Constant *ConstantZero = ConstantInt::get(CI->getOperand(0)->getType(), 0);
1326 Value *RunningOr = new ICmpInst(CI, ICmpInst::ICMP_SLT, CI->getOperand(0),
1325 Constant *ConstantZero = ConstantInt::get(CI->getOperand(1)->getType(), 0);
1326 Value *RunningOr = new ICmpInst(CI, ICmpInst::ICMP_SLT, CI->getOperand(1),
13271327 ConstantZero, "isneg");
13281328 for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) {
13291329 Value *Cond = new ICmpInst(CI, ICmpInst::ICMP_EQ, FieldMallocs[i],
15101510
15111511 // If this is an allocation of a fixed size array of structs, analyze as a
15121512 // variable size array. malloc [100 x struct],1 -> malloc struct, 100
1513 if (NElems == ConstantInt::get(CI->getOperand(0)->getType(), 1))
1513 if (NElems == ConstantInt::get(CI->getOperand(1)->getType(), 1))
15141514 if (const ArrayType *AT = dyn_cast(AllocTy))
15151515 AllocTy = AT->getElementType();
1516
1516
15171517 const StructType *AllocSTy = dyn_cast(AllocTy);
15181518 if (!AllocSTy)
15191519 return false;
16401640 // bool.
16411641 Instruction *StoredVal = cast(SI->getOperand(0));
16421642
1643 // If we've already replaced the input, StoredVal will be a cast or
1643 // If we're already replaced the input, StoredVal will be a cast or
16441644 // select instruction. If not, it will be a load of the original
16451645 // global.
16461646 if (LoadInst *LI = dyn_cast(StoredVal)) {
22612261 } else if (SelectInst *SI = dyn_cast(CurInst)) {
22622262 InstResult =
22632263 ConstantExpr::getSelect(getVal(Values, SI->getOperand(0)),
2264 getVal(Values, SI->getOperand(1)),
2265 getVal(Values, SI->getOperand(2)));
2264 getVal(Values, SI->getOperand(1)),
2265 getVal(Values, SI->getOperand(2)));
22662266 } else if (GetElementPtrInst *GEP = dyn_cast(CurInst)) {
22672267 Constant *P = getVal(Values, GEP->getOperand(0));
22682268 SmallVector GEPOps;
22942294 }
22952295
22962296 // Cannot handle inline asm.
2297 if (isa(CI->getCalledValue())) return false;
2297 if (isa(CI->getOperand(0))) return false;
22982298
22992299 // Resolve function pointers.
2300 Function *Callee = dyn_cast(getVal(Values, CI->getCalledValue()));
2300 Function *Callee = dyn_cast(getVal(Values, CI->getOperand(0)));
23012301 if (!Callee) return false; // Cannot resolve.
23022302
23032303 SmallVector Formals;
2304 for (User::op_iterator i = CI->op_begin(), e = CI->op_end() - 1;
2304 for (User::op_iterator i = CI->op_begin() + 1, e = CI->op_end();
23052305 i != e; ++i)
23062306 Formals.push_back(getVal(Values, *i));
23072307
261261 // char*. It returns "void", so it doesn't need to replace any of
262262 // Inst's uses and doesn't get a name.
263263 CastInst* CI =
264 new BitCastInst(Inst->getOperand(0), SBPTy, "LJBuf", Inst);
265 Value *Args[] = { CI, Inst->getOperand(1) };
264 new BitCastInst(Inst->getOperand(1), SBPTy, "LJBuf", Inst);
265 Value *Args[] = { CI, Inst->getOperand(2) };
266266 CallInst::Create(ThrowLongJmp, Args, Args + 2, "", Inst);
267267
268268 SwitchValuePair& SVP = SwitchValMap[Inst->getParent()->getParent()];
377377 const Type* SBPTy =
378378 Type::getInt8PtrTy(Inst->getContext());
379379 CastInst* BufPtr =
380 new BitCastInst(Inst->getOperand(0), SBPTy, "SBJmpBuf", Inst);
380 new BitCastInst(Inst->getOperand(1), SBPTy, "SBJmpBuf", Inst);
381381 Value *Args[] = {
382382 GetSetJmpMap(Func), BufPtr,
383383 ConstantInt::get(Type::getInt32Ty(Inst->getContext()), SetJmpIDMap[Func]++)
472472
473473 // Construct the new "invoke" instruction.
474474 TerminatorInst* Term = OldBB->getTerminator();
475 std::vector Params(CI.op_begin(), CI.op_end() - 1);
475 std::vector Params(CI.op_begin() + 1, CI.op_end());
476476 InvokeInst* II =
477477 InvokeInst::Create(CI.getCalledValue(), NewBB, PrelimBBMap[Func],
478478 Params.begin(), Params.end(), CI.getName(), Term);
108108 }
109109
110110 Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) {
111 unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(0));
112 unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(1));
111 unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1));
112 unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2));
113113 unsigned MinAlign = std::min(DstAlign, SrcAlign);
114114 unsigned CopyAlign = MI->getAlignment();
115115
121121
122122 // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
123123 // load/store.
124 ConstantInt *MemOpLength = dyn_cast(MI->getOperand(2));
124 ConstantInt *MemOpLength = dyn_cast(MI->getOperand(3));
125125 if (MemOpLength == 0) return 0;
126126
127127 // Source and destination pointer types are always "i8*" for intrinsic. See
136136
137137 // Use an integer load+store unless we can find something better.
138138 unsigned SrcAddrSp =
139 cast(MI->getOperand(2)->getType())->getAddressSpace();
140 unsigned DstAddrSp =
139141 cast(MI->getOperand(1)->getType())->getAddressSpace();
140 unsigned DstAddrSp =
141 cast(MI->getOperand(0)->getType())->getAddressSpace();
142142
143143 const IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3);
144144 Type *NewSrcPtrTy = PointerType::get(IntType, SrcAddrSp);
150150 // an i64 load+store, here because this improves the odds that the source or
151151 // dest address will be promotable. See if we can find a better type than the
152152 // integer datatype.
153 Value *StrippedDest = MI->getOperand(0)->stripPointerCasts();
154 if (StrippedDest != MI->getOperand(0)) {
153 Value *StrippedDest = MI->getOperand(1)->stripPointerCasts();
154 if (StrippedDest != MI->getOperand(1)) {
155155 const Type *SrcETy = cast(StrippedDest->getType())
156156 ->getElementType();
157157 if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) {
185185 SrcAlign = std::max(SrcAlign, CopyAlign);
186186 DstAlign = std::max(DstAlign, CopyAlign);
187187
188 Value *Src = Builder->CreateBitCast(MI->getOperand(1), NewSrcPtrTy);
189 Value *Dest = Builder->CreateBitCast(MI->getOperand(0), NewDstPtrTy);
188 Value *Src = Builder->CreateBitCast(MI->getOperand(2), NewSrcPtrTy);
189 Value *Dest = Builder->CreateBitCast(MI->getOperand(1), NewDstPtrTy);
190190 Instruction *L = new LoadInst(Src, "tmp", MI->isVolatile(), SrcAlign);
191191 InsertNewInstBefore(L, *MI);
192192 InsertNewInstBefore(new StoreInst(L, Dest, MI->isVolatile(), DstAlign),
193193 *MI);
194194
195195 // Set the size of the copy to 0, it will be deleted on the next iteration.
196 MI->setOperand(2, Constant::getNullValue(MemOpLength->getType()));
196 MI->setOperand(3, Constant::getNullValue(MemOpLength->getType()));
197197 return MI;
198198 }
199199
257257
258258 IntrinsicInst *II = dyn_cast(&CI);
259259 if (!II) return visitCallSite(&CI);
260
260
261261 // Intrinsics cannot occur in an invoke, so handle them here instead of in
262262 // visitCallSite.
263263 if (MemIntrinsic *MI = dyn_cast(II)) {
281281 if (MemMoveInst *MMI = dyn_cast(MI)) {
282282 if (GlobalVariable *GVSrc = dyn_cast(MMI->getSource()))
283283 if (GVSrc->isConstant()) {
284 Module *M = MMI->getParent()->getParent()->getParent();
284 Module *M = CI.getParent()->getParent()->getParent();
285285 Intrinsic::ID MemCpyID = Intrinsic::memcpy;
286 const Type *Tys[3] = { CI.getOperand(0)->getType(),
287 CI.getOperand(1)->getType(),
288 CI.getOperand(2)->getType() };
289 MMI->setCalledFunction(
286 const Type *Tys[3] = { CI.getOperand(1)->getType(),
287 CI.getOperand(2)->getType(),
288 CI.getOperand(3)->getType() };
289 CI.setOperand(0,
290290 Intrinsic::getDeclaration(M, MemCpyID, Tys, 3));
291291 Changed = true;
292292 }
296296 // memmove(x,x,size) -> noop.
297297 if (MTI->getSource() == MTI->getDest())
298298 return EraseInstFromFunction(CI);
299
300 // If we can determine a pointer alignment that is bigger than currently
301 // set, update the alignment.
302 if (Instruction *I = SimplifyMemTransfer(MTI))
299 }
300
301 // If we can determine a pointer alignment that is bigger than currently
302 // set, update the alignment.
303 if (isa(MI)) {
304 if (Instruction *I = SimplifyMemTransfer(MI))
303305 return I;
304306 } else if (MemSetInst *MSI = dyn_cast(MI)) {
305307 if (Instruction *I = SimplifyMemSet(MSI))
306308 return I;
307309 }
308
310
309311 if (Changed) return II;
310312 }
311
313
312314 switch (II->getIntrinsicID()) {
313315 default: break;
314316 case Intrinsic::objectsize: {
316318 if (!TD) break;
317319
318320 const Type *ReturnTy = CI.getType();
319 bool Min = (cast(II->getOperand(1))->getZExtValue() == 1);
321 bool Min = (cast(II->getOperand(2))->getZExtValue() == 1);
320322
321323 // Get to the real allocated thing and offset as fast as possible.
322 Value *Op1 = II->getOperand(0)->stripPointerCasts();
324 Value *Op1 = II->getOperand(1)->stripPointerCasts();
323325
324326 // If we've stripped down to a single global variable that we
325327 // can know the size of then just return that.
387389
388390 Constant *RetVal = ConstantInt::get(ReturnTy, Size-Offset);
389391 return ReplaceInstUsesWith(CI, RetVal);
392
390393 }
391394
392395 // Do not return "I don't know" here. Later optimization passes could
395398 }
396399 case Intrinsic::bswap:
397400 // bswap(bswap(x)) -> x
398 if (IntrinsicInst *Operand = dyn_cast(II->getOperand(0)))
401 if (IntrinsicInst *Operand = dyn_cast(II->getOperand(1)))
399402 if (Operand->getIntrinsicID() == Intrinsic::bswap)
400 return ReplaceInstUsesWith(CI, Operand->getOperand(0));
403 return ReplaceInstUsesWith(CI, Operand->getOperand(1));
401404
402405 // bswap(trunc(bswap(x))) -> trunc(lshr(x, c))
403 if (TruncInst *TI = dyn_cast(II->getOperand(0))) {
406 if (TruncInst *TI = dyn_cast(II->getOperand(1))) {
404407 if (IntrinsicInst *Operand = dyn_cast(TI->getOperand(0)))
405408 if (Operand->getIntrinsicID() == Intrinsic::bswap) {
406409 unsigned C = Operand->getType()->getPrimitiveSizeInBits() -
407410 TI->getType()->getPrimitiveSizeInBits();
408411 Value *CV = ConstantInt::get(Operand->getType(), C);
409 Value *V = Builder->CreateLShr(Operand->getOperand(0), CV);
412 Value *V = Builder->CreateLShr(Operand->getOperand(1), CV);
410413 return new TruncInst(V, TI->getType());
411414 }
412415 }
413416
414417 break;
415418 case Intrinsic::powi:
416 if (ConstantInt *Power = dyn_cast(II->getOperand(1))) {
419 if (ConstantInt *Power = dyn_cast(II->getOperand(2))) {
417420 // powi(x, 0) -> 1.0
418421 if (Power->isZero())
419422 return ReplaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0));
420423 // powi(x, 1) -> x
421424 if (Power->isOne())
422 return ReplaceInstUsesWith(CI, II->getOperand(0));
425 return ReplaceInstUsesWith(CI, II->getOperand(1));
423426 // powi(x, -1) -> 1/x
424427 if (Power->isAllOnesValue())
425428 return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0),
426 II->getOperand(0));
429 II->getOperand(1));
427430 }
428431 break;
429432 case Intrinsic::cttz: {
430433 // If all bits below the first known one are known zero,
431434 // this value is constant.
432 const IntegerType *IT = cast(II->getOperand(0)->getType());
435 const IntegerType *IT = cast(II->getOperand(1)->getType());
433436 uint32_t BitWidth = IT->getBitWidth();
434437 APInt KnownZero(BitWidth, 0);
435438 APInt KnownOne(BitWidth, 0);
436 ComputeMaskedBits(II->getOperand(0), APInt::getAllOnesValue(BitWidth),
439 ComputeMaskedBits(II->getOperand(1), APInt::getAllOnesValue(BitWidth),
437440 KnownZero, KnownOne);
438441 unsigned TrailingZeros = KnownOne.countTrailingZeros();
439442 APInt Mask(APInt::getLowBitsSet(BitWidth, TrailingZeros));
446449 case Intrinsic::ctlz: {
447450 // If all bits above the first known one are known zero,
448451 // this value is constant.
449 const IntegerType *IT = cast(II->getOperand(0)->getType());
452 const IntegerType *IT = cast(II->getOperand(1)->getType());
450453 uint32_t BitWidth = IT->getBitWidth();
451454 APInt KnownZero(BitWidth, 0);
452455 APInt KnownOne(BitWidth, 0);
453 ComputeMaskedBits(II->getOperand(0), APInt::getAllOnesValue(BitWidth),
456 ComputeMaskedBits(II->getOperand(1), APInt::getAllOnesValue(BitWidth),
454457 KnownZero, KnownOne);
455458 unsigned LeadingZeros = KnownOne.countLeadingZeros();
456459 APInt Mask(APInt::getHighBitsSet(BitWidth, LeadingZeros));
461464 }
462465 break;
463466 case Intrinsic::uadd_with_overflow: {
464 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
465 const IntegerType *IT = cast(II->getOperand(0)->getType());
467 Value *LHS = II->getOperand(1), *RHS = II->getOperand(2);
468 const IntegerType *IT = cast(II->getOperand(1)->getType());
466469 uint32_t BitWidth = IT->getBitWidth();
467470 APInt Mask = APInt::getSignBit(BitWidth);
468471 APInt LHSKnownZero(BitWidth, 0);
506509 // FALL THROUGH uadd into sadd
507510 case Intrinsic::sadd_with_overflow:
508511 // Canonicalize constants into the RHS.
509 if (isa(II->getOperand(0)) &&
510 !isa(II->getOperand(1))) {
511 Value *LHS = II->getOperand(0);
512 II->setOperand(0, II->getOperand(1));
513 II->setOperand(1, LHS);
512 if (isa(II->getOperand(1)) &&
513 !isa(II->getOperand(2))) {
514 Value *LHS = II->getOperand(1);
515 II->setOperand(1, II->getOperand(2));
516 II->setOperand(2, LHS);
514517 return II;
515518 }
516519
517520 // X + undef -> undef
518 if (isa(II->getOperand(1)))
521 if (isa(II->getOperand(2)))
519522 return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
520523
521 if (ConstantInt *RHS = dyn_cast(II->getOperand(1))) {
524 if (ConstantInt *RHS = dyn_cast(II->getOperand(2))) {
522525 // X + 0 -> {X, false}
523526 if (RHS->isZero()) {
524527 Constant *V[] = {
526529 ConstantInt::getFalse(II->getContext())
527530 };
528531 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
529 return InsertValueInst::Create(Struct, II->getOperand(0), 0);
532 return InsertValueInst::Create(Struct, II->getOperand(1), 0);
530533 }
531534 }
532535 break;
534537 case Intrinsic::ssub_with_overflow:
535538 // undef - X -> undef
536539 // X - undef -> undef
537 if (isa(II->getOperand(0)) ||
538 isa(II->getOperand(1)))
540 if (isa(II->getOperand(1)) ||
541 isa(II->getOperand(2)))
539542 return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
540543
541 if (ConstantInt *RHS = dyn_cast(II->getOperand(1))) {
544 if (ConstantInt *RHS = dyn_cast(II->getOperand(2))) {
542545 // X - 0 -> {X, false}
543546 if (RHS->isZero()) {
544547 Constant *V[] = {
545 UndefValue::get(II->getOperand(0)->getType()),
548 UndefValue::get(II->getOperand(1)->getType()),
546549 ConstantInt::getFalse(II->getContext())
547550 };
548551 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
549 return InsertValueInst::Create(Struct, II->getOperand(0), 0);
552 return InsertValueInst::Create(Struct, II->getOperand(1), 0);
550553 }
551554 }
552555 break;
553556 case Intrinsic::umul_with_overflow:
554557 case Intrinsic::smul_with_overflow:
555558 // Canonicalize constants into the RHS.
556 if (isa(II->getOperand(0)) &&
557 !isa(II->getOperand(1))) {
558 Value *LHS = II->getOperand(0);
559 II->setOperand(0, II->getOperand(1));
560 II->setOperand(1, LHS);
559 if (isa(II->getOperand(1)) &&
560 !isa(II->getOperand(2))) {
561 Value *LHS = II->getOperand(1);
562 II->setOperand(1, II->getOperand(2));
563 II->setOperand(2, LHS);
561564 return II;
562565 }
563566
564567 // X * undef -> undef
565 if (isa(II->getOperand(1)))
568 if (isa(II->getOperand(2)))
566569 return ReplaceInstUsesWith(CI, UndefValue::get(II->getType()));
567570
568 if (ConstantInt *RHSI = dyn_cast(II->getOperand(1))) {
571 if (ConstantInt *RHSI = dyn_cast(II->getOperand(2))) {
569572 // X*0 -> {0, false}
570573 if (RHSI->isZero())
571574 return ReplaceInstUsesWith(CI, Constant::getNullValue(II->getType()));
573576 // X * 1 -> {X, false}
574577 if (RHSI->equalsInt(1)) {
575578 Constant *V[] = {
576 UndefValue::get(II->getOperand(0)->getType()),
579 UndefValue::get(II->getOperand(1)->getType()),
577580 ConstantInt::getFalse(II->getContext())
578581 };
579582 Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false);
580 return InsertValueInst::Create(Struct, II->getOperand(0), 0);
583 return InsertValueInst::Create(Struct, II->getOperand(1), 0);
581584 }
582585 }
583586 break;
588591 case Intrinsic::x86_sse2_loadu_dq:
589592 // Turn PPC lvx -> load if the pointer is known aligned.
590593 // Turn X86 loadups -> load if the pointer is known aligned.
591 if (GetOrEnforceKnownAlignment(II->getOperand(0), 16) >= 16) {
592 Value *Ptr = Builder->CreateBitCast(II->getOperand(0),
594 if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
595 Value *Ptr = Builder->CreateBitCast(II->getOperand(1),
593596 PointerType::getUnqual(II->getType()));
594597 return new LoadInst(Ptr);
595598 }
597600 case Intrinsic::ppc_altivec_stvx:
598601 case Intrinsic::ppc_altivec_stvxl:
599602 // Turn stvx -> store if the pointer is known aligned.
600 if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
603 if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) {
601604 const Type *OpPtrTy =
602 PointerType::getUnqual(II->getOperand(0)->getType());
603 Value *Ptr = Builder->CreateBitCast(II->getOperand(1), OpPtrTy);
604 return new StoreInst(II->getOperand(0), Ptr);
605 PointerType::getUnqual(II->getOperand(1)->getType());
606 Value *Ptr = Builder->CreateBitCast(II->getOperand(2), OpPtrTy);
607 return new StoreInst(II->getOperand(1), Ptr);
605608 }
606609 break;
607610 case Intrinsic::x86_sse_storeu_ps:
608611 case Intrinsic::x86_sse2_storeu_pd:
609612 case Intrinsic::x86_sse2_storeu_dq:
610613 // Turn X86 storeu -> store if the pointer is known aligned.
611 if (GetOrEnforceKnownAlignment(II->getOperand(0), 16) >= 16) {
614 if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) {
612615 const Type *OpPtrTy =
613 PointerType::getUnqual(II->getOperand(1)->getType());
614 Value *Ptr = Builder->CreateBitCast(II->getOperand(0), OpPtrTy);
615 return new StoreInst(II->getOperand(1), Ptr);
616 PointerType::getUnqual(II->getOperand(2)->getType());
617 Value *Ptr = Builder->CreateBitCast(II->getOperand(1), OpPtrTy);
618 return new StoreInst(II->getOperand(2), Ptr);
616619 }
617620 break;
618621
620623 // These intrinsics only demands the 0th element of its input vector. If
621624 // we can simplify the input based on that, do so now.
622625 unsigned VWidth =
623 cast(II->getOperand(0)->getType())->getNumElements();
626 cast(II->getOperand(1)->getType())->getNumElements();
624627 APInt DemandedElts(VWidth, 1);
625628 APInt UndefElts(VWidth, 0);
626 if (Value *V = SimplifyDemandedVectorElts(II->getOperand(0), DemandedElts,
629 if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts,
627630 UndefElts)) {
628 II->setOperand(0, V);
631 II->setOperand(1, V);
629632 return II;
630633 }
631634 break;
633636
634637 case Intrinsic::ppc_altivec_vperm:
635638 // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
636 if (ConstantVector *Mask = dyn_cast(II->getOperand(2))) {
639 if (ConstantVector *Mask = dyn_cast(II->getOperand(3))) {
637640 assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!");
638641
639642 // Check that all of the elements are integer constants or undefs.
648651
649652 if (AllEltsOk) {
650653 // Cast the input vectors to byte vectors.
651 Value *Op0 = Builder->CreateBitCast(II->getOperand(0), Mask->getType());
652 Value *Op1 = Builder->CreateBitCast(II->getOperand(1), Mask->getType());
654 Value *Op0 = Builder->CreateBitCast(II->getOperand(1), Mask->getType());
655 Value *Op1 = Builder->CreateBitCast(II->getOperand(2), Mask->getType());
653656 Value *Result = UndefValue::get(Op0->getType());
654657
655658 // Only extract each element once.
682685 case Intrinsic::stackrestore: {
683686 // If the save is right next to the restore, remove the restore. This can
684687 // happen when variable allocas are DCE'd.
685 if (IntrinsicInst *SS = dyn_cast(II->getOperand(0))) {
688 if (IntrinsicInst *SS = dyn_cast(II->getOperand(1))) {
686689 if (SS->getIntrinsicID() == Intrinsic::stacksave) {
687690 BasicBlock::iterator BI = SS;
688691 if (&*++BI == II)
839842 UndefValue::get(Type::getInt1PtrTy(Callee->getContext())),
840843 CS.getInstruction());
841844
842 // If CS does not return void then replaceAllUsesWith undef.
845 // If CS dues not return void then replaceAllUsesWith undef.
843846 // This allows ValueHandlers and custom metadata to adjust itself.
844847 if (!CS.getInstruction()->getType()->isVoidTy())
845848 CS.getInstruction()->
11331136 IntrinsicInst *Tramp =
11341137 cast(cast(Callee)->getOperand(0));
11351138
1136 Function *NestF = cast(Tramp->getOperand(1)->stripPointerCasts());
1139 Function *NestF = cast(Tramp->getOperand(2)->stripPointerCasts());
11371140 const PointerType *NestFPTy = cast(NestF->getType());
11381141 const FunctionType *NestFTy = cast(NestFPTy->getElementType());
11391142
11741177 do {
11751178 if (Idx == NestIdx) {
11761179 // Add the chain argument and attributes.
1177 Value *NestVal = Tramp->getOperand(2);
1180 Value *NestVal = Tramp->getOperand(3);
11781181 if (NestVal->getType() != NestTy)
11791182 NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller);
11801183 NewArgs.push_back(NestVal);
14221422 switch (II->getIntrinsicID()) {
14231423 case Intrinsic::bswap:
14241424 Worklist.Add(II);
1425 ICI.setOperand(0, II->getOperand(0));
1425 ICI.setOperand(0, II->getOperand(1));
14261426 ICI.setOperand(1, ConstantInt::get(II->getContext(), RHSV.byteSwap()));
14271427 return &ICI;
14281428 case Intrinsic::ctlz:
14301430 // ctz(A) == bitwidth(a) -> A == 0 and likewise for !=
14311431 if (RHSV == RHS->getType()->getBitWidth()) {
14321432 Worklist.Add(II);
1433 ICI.setOperand(0, II->getOperand(0));
1433 ICI.setOperand(0, II->getOperand(1));
14341434 ICI.setOperand(1, ConstantInt::get(RHS->getType(), 0));
14351435 return &ICI;
14361436 }
14391439 // popcount(A) == 0 -> A == 0 and likewise for !=
14401440 if (RHS->isZero()) {
14411441 Worklist.Add(II);
1442 ICI.setOperand(0, II->getOperand(0));
1442 ICI.setOperand(0, II->getOperand(1));
14431443 ICI.setOperand(1, RHS);
14441444 return &ICI;
14451445 }
403403 isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == Op1C->getZExtValue()){
404404 bool isCtPop = II->getIntrinsicID() == Intrinsic::ctpop;
405405 Constant *RHS = ConstantInt::getSigned(Op0->getType(), isCtPop ? -1:0);
406 Value *Cmp = Builder->CreateICmpEQ(II->getOperand(0), RHS);
406 Value *Cmp = Builder->CreateICmpEQ(II->getOperand(1), RHS);
407407 return new ZExtInst(Cmp, II->getType());
408408 }
409409 }
731731 // the right place.
732732 Instruction *NewVal;
733733 if (InputBit > ResultBit)
734 NewVal = BinaryOperator::CreateLShr(II->getOperand(0),
734 NewVal = BinaryOperator::CreateLShr(I->getOperand(1),
735735 ConstantInt::get(I->getType(), InputBit-ResultBit));
736736 else
737 NewVal = BinaryOperator::CreateShl(II->getOperand(0),
737 NewVal = BinaryOperator::CreateShl(I->getOperand(1),
738738 ConstantInt::get(I->getType(), ResultBit-InputBit));
739739 NewVal->takeName(I);
740740 return InsertNewInstBefore(NewVal, *I);
10511051 case Intrinsic::x86_sse2_mul_sd:
10521052 case Intrinsic::x86_sse2_min_sd:
10531053 case Intrinsic::x86_sse2_max_sd:
1054 TmpV = SimplifyDemandedVectorElts(II->getOperand(0), DemandedElts,
1054 TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts,
10551055 UndefElts, Depth+1);
1056 if (TmpV) { II->setOperand(0, TmpV); MadeChange = true; }
1057 TmpV = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts,
1056 if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; }
1057 TmpV = SimplifyDemandedVectorElts(II->getOperand(2), DemandedElts,
10581058 UndefElts2, Depth+1);
1059 if (TmpV) { II->setOperand(1, TmpV); MadeChange = true; }
1059 if (TmpV) { II->setOperand(2, TmpV); MadeChange = true; }
10601060
10611061 // If only the low elt is demanded and this is a scalarizable intrinsic,
10621062 // scalarize it now.
10681068 case Intrinsic::x86_sse2_sub_sd:
10691069 case Intrinsic::x86_sse2_mul_sd:
10701070 // TODO: Lower MIN/MAX/ABS/etc
1071 Value *LHS = II->getOperand(0);
1072 Value *RHS = II->getOperand(1);
1071 Value *LHS = II->getOperand(1);
1072 Value *RHS = II->getOperand(2);
10731073 // Extract the element as scalars.
10741074 LHS = InsertNewInstBefore(ExtractElementInst::Create(LHS,
10751075 ConstantInt::get(Type::getInt32Ty(I->getContext()), 0U)), *II);
710710 }
711711
712712 Instruction *InstCombiner::visitFree(Instruction &FI) {
713 Value *Op = FI.getOperand(0);
713 Value *Op = FI.getOperand(1);
714714
715715 // free undef -> unreachable.
716716 if (isa(Op)) {
895895 if (IntrinsicInst *II = dyn_cast(Agg)) {
896896 // We're extracting from an intrinsic, see if we're the only user, which
897897 // allows us to simplify multiple result intrinsics to simpler things that
898 // just get one value.
898 // just get one value..
899899 if (II->hasOneUse()) {
900900 // Check if we're grabbing the overflow bit or the result of a 'with
901901 // overflow' intrinsic. If it's the latter we can remove the intrinsic
904904 case Intrinsic::uadd_with_overflow:
905905 case Intrinsic::sadd_with_overflow:
906906 if (*EV.idx_begin() == 0) { // Normal result.
907 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
907 Value *LHS = II->getOperand(1), *RHS = II->getOperand(2);
908908 II->replaceAllUsesWith(UndefValue::get(II->getType()));
909909 EraseInstFromFunction(*II);
910910 return BinaryOperator::CreateAdd(LHS, RHS);
913913 case Intrinsic::usub_with_overflow:
914914 case Intrinsic::ssub_with_overflow:
915915 if (*EV.idx_begin() == 0) { // Normal result.
916 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
916 Value *LHS = II->getOperand(1), *RHS = II->getOperand(2);
917917 II->replaceAllUsesWith(UndefValue::get(II->getType()));
918918 EraseInstFromFunction(*II);
919919 return BinaryOperator::CreateSub(LHS, RHS);
922922 case Intrinsic::umul_with_overflow:
923923 case Intrinsic::smul_with_overflow:
924924 if (*EV.idx_begin() == 0) { // Normal result.
925 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
925 Value *LHS = II->getOperand(1), *RHS = II->getOperand(2);
926926 II->replaceAllUsesWith(UndefValue::get(II->getType()));
927927 EraseInstFromFunction(*II);
928928 return BinaryOperator::CreateMul(LHS, RHS);
7272 if (AI->getType() != ArgVTy) {
7373 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
7474 false);
75 InitCall->setOperand(1,
75 InitCall->setOperand(2,
7676 CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall));
7777 } else {
78 InitCall->setOperand(1, AI);
78 InitCall->setOperand(2, AI);
7979 }
8080 /* FALL THROUGH */
8181
9292 }
9393 opcode = CastInst::getCastOpcode(AI, true,
9494 Type::getInt32Ty(Context), true);
95 InitCall->setOperand(0,
95 InitCall->setOperand(1,
9696 CastInst::Create(opcode, AI, Type::getInt32Ty(Context),
9797 "argc.cast", InitCall));
9898 } else {
9999 AI->replaceAllUsesWith(InitCall);
100 InitCall->setOperand(0, AI);
100 InitCall->setOperand(1, AI);
101101 }
102102
103103 case 0: break;
558558 // Lower all uses of llvm.objectsize.*
559559 IntrinsicInst *II = dyn_cast(CI);
560560 if (II && II->getIntrinsicID() == Intrinsic::objectsize) {
561 bool Min = (cast(II->getOperand(1))->getZExtValue() == 1);
561 bool Min = (cast(II->getOperand(2))->getZExtValue() == 1);
562562 const Type *ReturnTy = CI->getType();
563563 Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);
564564 CI->replaceAllUsesWith(RetVal);
122122 if (StoreInst *SI = dyn_cast(I))
123123 return SI->getPointerOperand();
124124 if (MemIntrinsic *MI = dyn_cast(I))
125 return MI->getOperand(0);
125 return MI->getOperand(1);
126126
127127 switch (cast(I)->getIntrinsicID()) {
128128 default: assert(false && "Unexpected intrinsic!");
129129 case Intrinsic::init_trampoline:
130 return I->getOperand(0);
130 return I->getOperand(1);
131131 case Intrinsic::lifetime_end:
132 return I->getOperand(1);
132 return I->getOperand(2);
133133 }
134134 }
135135
151151 case Intrinsic::init_trampoline:
152152 return -1u;
153153 case Intrinsic::lifetime_end:
154 Len = I->getOperand(0);
154 Len = I->getOperand(1);
155155 break;
156156 }
157157 }
286286
287287 /// handleFreeWithNonTrivialDependency - Handle frees of entire structures whose
288288 /// dependency is a store to a field of that structure.
289 bool DSE::handleFreeWithNonTrivialDependency(/*FIXME: Call*/Instruction *F, MemDepResult Dep) {
289 bool DSE::handleFreeWithNonTrivialDependency(Instruction *F, MemDepResult Dep) {
290290 AliasAnalysis &AA = getAnalysis();
291291
292292 Instruction *Dependency = Dep.getInst();
296296 Value *DepPointer = getPointerOperand(Dependency)->getUnderlyingObject();
297297
298298 // Check for aliasing.
299 if (AA.alias(F->getOperand(0), 1, DepPointer, 1) !=
299 if (AA.alias(F->getOperand(1), 1, DepPointer, 1) !=
300300 AliasAnalysis::MustAlias)
301301 return false;
302302
270270 e.function = C->getCalledFunction();
271271 e.opcode = Expression::CALL;
272272
273 for (CallInst::op_iterator I = C->op_begin(), E = C->op_end() - 1;
273 for (CallInst::op_iterator I = C->op_begin()+1, E = C->op_end();
274274 I != E; ++I)
275275 e.varargs.push_back(lookup_or_add(*I));
276276
451451 return nextValueNumber++;
452452 }
453453
454 for (unsigned i = 0, e = C->getNumOperands() - 1; i < e; ++i) {
454 for (unsigned i = 1; i < C->getNumOperands(); ++i) {
455455 uint32_t c_vn = lookup_or_add(C->getOperand(i));
456456 uint32_t cd_vn = lookup_or_add(local_cdep->getOperand(i));
457457 if (c_vn != cd_vn) {
507507 valueNumbering[C] = nextValueNumber;
508508 return nextValueNumber++;
509509 }
510 for (unsigned i = 0, e = C->getNumOperands() - 1; i < e; ++i) {
510 for (unsigned i = 1; i < C->getNumOperands(); ++i) {
511511 uint32_t c_vn = lookup_or_add(C->getOperand(i));
512512 uint32_t cd_vn = lookup_or_add(cdep->getOperand(i));
513513 if (c_vn != cd_vn) {
743743 const Type *ArgTys[3] = { M->getRawDest()->getType(),
744744 M->getRawSource()->getType(),
745745 M->getLength()->getType() };
746 M->setCalledFunction(Intrinsic::getDeclaration(Mod, Intrinsic::memcpy, ArgTys, 3));
746 M->setOperand(0,Intrinsic::getDeclaration(Mod, Intrinsic::memcpy, ArgTys, 3));
747747
748748 // MemDep may have over conservative information about this instruction, just
749749 // conservatively flush it from the cache.
964964 isSafeGEP(GEPI, AI, GEPOffset, Info);
965965 if (!Info.isUnsafe)
966966 isSafeForScalarRepl(GEPI, AI, GEPOffset, Info);
967 } else if (MemIntrinsic *MI = dyn_cast(User)) {
967 } else if (MemIntrinsic *MI = dyn_cast(UI)) {
968968 ConstantInt *Length = dyn_cast(MI->getLength());
969969 if (Length)
970970 isSafeMemAccess(AI, Offset, Length->getZExtValue(), 0,
971 UI.getOperandNo() == 0, Info);
971 UI.getOperandNo() == 1, Info);
972972 else
973973 MarkUnsafe(Info);
974974 } else if (LoadInst *LI = dyn_cast(User)) {
13151315 }
13161316
13171317 // Process each element of the aggregate.
1318 Value *TheFn = MI->getCalledValue();
1318 Value *TheFn = MI->getOperand(0);
13191319 const Type *BytePtrTy = MI->getRawDest()->getType();
13201320 bool SROADest = MI->getRawDest() == Inst;
13211321
13721372 // If the stored element is zero (common case), just store a null
13731373 // constant.
13741374 Constant *StoreVal;
1375 if (ConstantInt *CI = dyn_cast(MI->getOperand(1))) {
1375 if (ConstantInt *CI = dyn_cast(MI->getOperand(2))) {
13761376 if (CI->isZero()) {
13771377 StoreVal = Constant::getNullValue(EltTy); // 0.0, null, 0, <0,0>
13781378 } else {
14351435 Value *Ops[] = {
14361436 SROADest ? EltPtr : OtherElt, // Dest ptr
14371437 SROADest ? OtherElt : EltPtr, // Src ptr
1438 ConstantInt::get(MI->getOperand(2)->getType(), EltSize), // Size
1438 ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size
14391439 // Align
14401440 ConstantInt::get(Type::getInt32Ty(MI->getContext()), OtherEltAlign),
14411441 MI->getVolatileCst()
14501450 } else {
14511451 assert(isa(MI));
14521452 Value *Ops[] = {
1453 EltPtr, MI->getOperand(1), // Dest, Value,
1454 ConstantInt::get(MI->getOperand(2)->getType(), EltSize), // Size
1453 EltPtr, MI->getOperand(2), // Dest, Value,
1454 ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size
14551455 Zero, // Align
14561456 ConstantInt::get(Type::getInt1Ty(MI->getContext()), 0) // isVolatile
14571457 };
17931793 if (isOffset) return false;
17941794
17951795 // If the memintrinsic isn't using the alloca as the dest, reject it.
1796 if (UI.getOperandNo() != 0) return false;
1796 if (UI.getOperandNo() != 1) return false;
17971797
17981798 // If the source of the memcpy/move is not a constant global, reject it.
17991799 if (!PointsToConstantGlobal(MI->getSource()))
109109 return 0;
110110
111111 // Extract some information from the instruction
112 Value *Dst = CI->getOperand(0);
113 Value *Src = CI->getOperand(1);
112 Value *Dst = CI->getOperand(1);
113 Value *Src = CI->getOperand(2);
114114
115115 // See if we can get the length of the input string.
116116 uint64_t Len = GetStringLength(Src);
161161 return 0;
162162
163163 // Extract some information from the instruction
164 Value *Dst = CI->getOperand(0);
165 Value *Src = CI->getOperand(1);
164 Value *Dst = CI->getOperand(1);
165 Value *Src = CI->getOperand(2);
166166 uint64_t Len;
167167
168168 // We don't do anything if length is not constant
169 if (ConstantInt *LengthArg = dyn_cast(CI->getOperand(2)))
169 if (ConstantInt *LengthArg = dyn_cast(CI->getOperand(3)))
170170 Len = LengthArg->getZExtValue();
171171 else
172172 return 0;
206206 FT->getParamType(0) != FT->getReturnType())
207207 return 0;
208208
209 Value *SrcStr = CI->getOperand(0);
209 Value *SrcStr = CI->getOperand(1);
210210
211211 // If the second operand is non-constant, see if we can compute the length
212212 // of the input string and turn this into memchr.
213 ConstantInt *CharC = dyn_cast(CI->getOperand(1));
213 ConstantInt *CharC = dyn_cast(CI->getOperand(2));
214214 if (CharC == 0) {
215215 // These optimizations require TargetData.
216216 if (!TD) return 0;
219219 if (Len == 0 || !FT->getParamType(1)->isIntegerTy(32))// memchr needs i32.
220220 return 0;
221221
222 return EmitMemChr(SrcStr, CI->getOperand(1), // include nul.
222 return EmitMemChr(SrcStr, CI->getOperand(2), // include nul.
223223 ConstantInt::get(TD->getIntPtrType(*Context), Len),
224224 B, TD);
225225 }
264264 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
265265 return 0;
266266
267 Value *Str1P = CI->getOperand(0), *Str2P = CI->getOperand(1);
267 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
268268 if (Str1P == Str2P) // strcmp(x,x) -> 0
269269 return ConstantInt::get(CI->getType(), 0);
270270
313313 !FT->getParamType(2)->isIntegerTy())
314314 return 0;
315315
316 Value *Str1P = CI->getOperand(0), *Str2P = CI->getOperand(1);
316 Value *Str1P = CI->getOperand(1), *Str2P = CI->getOperand(2);
317317 if (Str1P == Str2P) // strncmp(x,x,n) -> 0
318318 return ConstantInt::get(CI->getType(), 0);
319319
320320 // Get the length argument if it is constant.
321321 uint64_t Length;
322 if (ConstantInt *LengthArg = dyn_cast(CI->getOperand(2)))
322 if (ConstantInt *LengthArg = dyn_cast(CI->getOperand(3)))
323323 Length = LengthArg->getZExtValue();
324324 else
325325 return 0;
364364 FT->getParamType(0) != Type::getInt8PtrTy(*Context))
365365 return 0;
366366
367 Value *Dst = CI->getOperand(0), *Src = CI->getOperand(1);
367 Value *Dst = CI->getOperand(1), *Src = CI->getOperand(2);
368368 if (Dst == Src) // strcpy(x,x) -> x
369369 return Src;
370370
380380 if (OptChkCall)
381381 EmitMemCpyChk(Dst, Src,
382382 ConstantInt::get(TD->getIntPtrType(*Context), Len),
383 CI->getOperand(2), B, TD);
383 CI->getOperand(3), B, TD);
384384 else
385385 EmitMemCpy(Dst, Src,
386386 ConstantInt::get(TD->getIntPtrType(*Context), Len),
401401 !FT->getParamType(2)->isIntegerTy())
402402 return 0;
403403
404 Value *Dst = CI->getOperand(0);
405 Value *Src = CI->getOperand(1);
406 Value *LenOp = CI->getOperand(2);
404 Value *Dst = CI->getOperand(1);
405 Value *Src = CI->getOperand(2);
406 Value *LenOp = CI->getOperand(3);
407407
408408 // See if we can get the length of the input string.
409409 uint64_t SrcLen = GetStringLength(Src);
451451 !FT->getReturnType()->isIntegerTy())
452452 return 0;
453453
454 Value *Src = CI->getOperand(0);
454 Value *Src = CI->getOperand(1);
455455
456456 // Constant folding: strlen("xyz") -> 3
457457 if (uint64_t Len = GetStringLength(Src))
476476 !FT->getParamType(1)->isPointerTy())
477477 return 0;
478478
479 Value *EndPtr = CI->getOperand(1);
479 Value *EndPtr = CI->getOperand(2);
480480 if (isa(EndPtr)) {
481481 CI->setOnlyReadsMemory();
482482 CI->addAttribute(1, Attribute::NoCapture);
499499 return 0;
500500
501501 // fold strstr(x, x) -> x.
502 if (CI->getOperand(0) == CI->getOperand(1))
503 return B.CreateBitCast(CI->getOperand(0), CI->getType());
502 if (CI->getOperand(1) == CI->getOperand(2))
503 return B.CreateBitCast(CI->getOperand(1), CI->getType());
504504
505505 // See if either input string is a constant string.
506506 std::string SearchStr, ToFindStr;
507 bool HasStr1 = GetConstantStringInfo(CI->getOperand(0), SearchStr);
508 bool HasStr2 = GetConstantStringInfo(CI->getOperand(1), ToFindStr);
507 bool HasStr1 = GetConstantStringInfo(CI->getOperand(1), SearchStr);
508 bool HasStr2 = GetConstantStringInfo(CI->getOperand(2), ToFindStr);
509509
510510 // fold strstr(x, "") -> x.
511511 if (HasStr2 && ToFindStr.empty())
512 return B.CreateBitCast(CI->getOperand(0), CI->getType());
512 return B.CreateBitCast(CI->getOperand(1), CI->getType());
513513
514514 // If both strings are known, constant fold it.
515515 if (HasStr1 && HasStr2) {
519519 return Constant::getNullValue(CI->getType());
520520
521521 // strstr("abcd", "bc") -> gep((char*)"abcd", 1)
522 Value *Result = CastToCStr(CI->getOperand(0), B);
522 Value *Result = CastToCStr(CI->getOperand(1), B);
523523 Result = B.CreateConstInBoundsGEP1_64(Result, Offset, "strstr");
524524 return B.CreateBitCast(Result, CI->getType());
525525 }
526526
527527 // fold strstr(x, "y") -> strchr(x, 'y').
528528 if (HasStr2 && ToFindStr.size() == 1)
529 return B.CreateBitCast(EmitStrChr(CI->getOperand(0), ToFindStr[0], B, TD),
529 return B.CreateBitCast(EmitStrChr(CI->getOperand(1), ToFindStr[0], B, TD),
530530 CI->getType());
531531 return 0;
532532 }
544544 !FT->getReturnType()->isIntegerTy(32))
545545 return 0;
546546
547 Value *LHS = CI->getOperand(0), *RHS = CI->getOperand(1);
547 Value *LHS = CI->getOperand(1), *RHS = CI->getOperand(2);
548548
549549 if (LHS == RHS) // memcmp(s,s,x) -> 0
550550 return Constant::getNullValue(CI->getType());
551551
552552 // Make sure we have a constant length.
553 ConstantInt *LenC = dyn_cast(CI->getOperand(2));
553 ConstantInt *LenC = dyn_cast(CI->getOperand(3));
554554 if (!LenC) return 0;
555555 uint64_t Len = LenC->getZExtValue();
556556
594594 return 0;
595595
596596 // memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
597 EmitMemCpy(CI->getOperand(0), CI->getOperand(1),
598 CI->getOperand(2), 1, false, B, TD);
599 return CI->getOperand(0);
597 EmitMemCpy(CI->getOperand(1), CI->getOperand(2),
598 CI->getOperand(3), 1, false, B, TD);
599 return CI->getOperand(1);
600600 }
601601 };
602602
616616 return 0;
617617
618618 // memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
619 EmitMemMove(CI->getOperand(0), CI->getOperand(1),
620 CI->getOperand(2), 1, false, B, TD);
621 return CI->getOperand(0);
619 EmitMemMove(CI->getOperand(1), CI->getOperand(2),
620 CI->getOperand(3), 1, false, B, TD);
621 return CI->getOperand(1);
622622 }
623623 };
624624
638638 return 0;
639639
640640 // memset(p, v, n) -> llvm.memset(p, v, n, 1)
641 Value *Val = B.CreateIntCast(CI->getOperand(1), Type::getInt8Ty(*Context),
642 false);
643 EmitMemSet(CI->getOperand(0), Val, CI->getOperand(2), false, B, TD);
644 return CI->getOperand(0);
641 Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
642 false);
643 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), false, B, TD);
644 return CI->getOperand(1);
645645 }
646646 };
647647
662662 !FT->getParamType(0)->isFloatingPointTy())
663663 return 0;
664664
665 Value *Op1 = CI->getOperand(0), *Op2 = CI->getOperand(1);
665 Value *Op1 = CI->getOperand(1), *Op2 = CI->getOperand(2);
666666 if (ConstantFP *Op1C = dyn_cast(Op1)) {
667667 if (Op1C->isExactlyValue(1.0)) // pow(1.0, x) -> 1.0
668668 return Op1C;
716716 !FT->getParamType(0)->isFloatingPointTy())
717717 return 0;
718718
719 Value *Op = CI->getOperand(0);
719 Value *Op = CI->getOperand(1);
720720 // Turn exp2(sitofp(x)) -> ldexp(1.0, sext(x)) if sizeof(x) <= 32
721721 // Turn exp2(uitofp(x)) -> ldexp(1.0, zext(x)) if sizeof(x) < 32
722722 Value *LdExpArg = 0;
768768 return 0;
769769
770770 // If this is something like 'floor((double)floatval)', convert to floorf.
771 FPExtInst *Cast = dyn_cast(CI->getOperand(0));
771 FPExtInst *Cast = dyn_cast(CI->getOperand(1));
772772 if (Cast == 0 || !Cast->getOperand(0)->getType()->isFloatTy())
773773 return 0;
774774
797797 !FT->getParamType(0)->isIntegerTy())
798798 return 0;
799799
800 Value *Op = CI->getOperand(0);
800 Value *Op = CI->getOperand(1);
801801
802802 // Constant fold.
803803 if (ConstantInt *CI = dyn_cast(Op)) {
833833 return 0;
834834
835835 // isdigit(c) -> (c-'0')
836 Value *Op = CI->getOperand(0);
836 Value *Op = CI->getOperand(1);
837837 Op = B.CreateSub(Op, ConstantInt::get(Type::getInt32Ty(*Context), '0'),
838838 "isdigittmp");
839839 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 10),
854854 return 0;
855855
856856 // isascii(c) -> c
857 Value *Op = CI->getOperand(0);
857 Value *Op = CI->getOperand(1);
858858 Op = B.CreateICmpULT(Op, ConstantInt::get(Type::getInt32Ty(*Context), 128),
859859 "isascii");
860860 return B.CreateZExt(Op, CI->getType());
873873 return 0;
874874
875875 // abs(x) -> x >s -1 ? x : -x
876 Value *Op = CI->getOperand(0);
876 Value *Op = CI->getOperand(1);
877877 Value *Pos = B.CreateICmpSGT(Op,
878878 Constant::getAllOnesValue(Op->getType()),
879879 "ispos");
895895 return 0;
896896
897897 // isascii(c) -> c & 0x7f
898 return B.CreateAnd(CI->getOperand(0),
898 return B.CreateAnd(CI->getOperand(1),
899899 ConstantInt::get(CI->getType(),0x7F));
900900 }
901901 };
918918
919919 // Check for a fixed format string.
920920 std::string FormatStr;
921 if (!GetConstantStringInfo(CI->getOperand(0), FormatStr))
921 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
922922 return 0;
923923
924924 // Empty format string -> noop.
950950 }
951951
952952 // Optimize specific format strings.
953 // printf("%c", chr) --> putchar(chr)
953 // printf("%c", chr) --> putchar(*(i8*)dst)
954954 if (FormatStr == "%c" && CI->getNumOperands() > 2 &&
955 CI->getOperand(1)->getType()->isIntegerTy()) {
956 Value *Res = EmitPutChar(CI->getOperand(1), B, TD);
955 CI->getOperand(2)->getType()->isIntegerTy()) {
956 Value *Res = EmitPutChar(CI->getOperand(2), B, TD);
957957
958958 if (CI->use_empty()) return CI;
959959 return B.CreateIntCast(Res, CI->getType(), true);
961961
962962 // printf("%s\n", str) --> puts(str)
963963 if (FormatStr == "%s\n" && CI->getNumOperands() > 2 &&
964 CI->getOperand(1)->getType()->isPointerTy() &&
964 CI->getOperand(2)->getType()->isPointerTy() &&
965965 CI->use_empty()) {
966 EmitPutS(CI->getOperand(1), B, TD);
966 EmitPutS(CI->getOperand(2), B, TD);
967967 return CI;
968968 }
969969 return 0;
984984
985985 // Check for a fixed format string.
986986 std::string FormatStr;
987 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
987 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
988988 return 0;
989989
990990 // If we just have a format string (nothing else crazy) transform it.
999999 if (!TD) return 0;
10001000
10011001 // sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
1002 EmitMemCpy(CI->getOperand(0), CI->getOperand(1), // Copy the nul byte.
1002 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
10031003 ConstantInt::get(TD->getIntPtrType(*Context),
10041004 FormatStr.size()+1), 1, false, B, TD);
10051005 return ConstantInt::get(CI->getType(), FormatStr.size());
10131013 // Decode the second character of the format string.
10141014 if (FormatStr[1] == 'c') {
10151015 // sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
1016 if (!CI->getOperand(2)->getType()->isIntegerTy()) return 0;
1017 Value *V = B.CreateTrunc(CI->getOperand(2),
1016 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
1017 Value *V = B.CreateTrunc(CI->getOperand(3),
10181018 Type::getInt8Ty(*Context), "char");
1019 Value *Ptr = CastToCStr(CI->getOperand(0), B);
1019 Value *Ptr = CastToCStr(CI->getOperand(1), B);
10201020 B.CreateStore(V, Ptr);
10211021 Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
10221022 "nul");
10301030 if (!TD) return 0;
10311031
10321032 // sprintf(dest, "%s", str) -> llvm.memcpy(dest, str, strlen(str)+1, 1)
1033 if (!CI->getOperand(2)->getType()->isPointerTy()) return 0;
1034
1035 Value *Len = EmitStrLen(CI->getOperand(2), B, TD);
1033 if (!CI->getOperand(3)->getType()->isPointerTy()) return 0;
1034
1035 Value *Len = EmitStrLen(CI->getOperand(3), B, TD);
10361036 Value *IncLen = B.CreateAdd(Len,
10371037 ConstantInt::get(Len->getType(), 1),
10381038 "leninc");
1039 EmitMemCpy(CI->getOperand(0), CI->getOperand(2), IncLen, 1, false, B, TD);
1039 EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, false, B, TD);
10401040
10411041 // The sprintf result is the unincremented number of bytes in the string.
10421042 return B.CreateIntCast(Len, CI->getType(), false);
10601060 return 0;
10611061
10621062 // Get the element size and count.
1063 ConstantInt *SizeC = dyn_cast(CI->getOperand(1));
1064 ConstantInt *CountC = dyn_cast(CI->getOperand(2));
1063 ConstantInt *SizeC = dyn_cast(CI->getOperand(2));
1064 ConstantInt *CountC = dyn_cast(CI->getOperand(3));
10651065 if (!SizeC || !CountC) return 0;
10661066 uint64_t Bytes = SizeC->getZExtValue()*CountC->getZExtValue();
10671067
10711071
10721072 // If this is writing one byte, turn it into fputc.
10731073 if (Bytes == 1) { // fwrite(S,1,1,F) -> fputc(S[0],F)
1074 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(0), B), "char");
1075 EmitFPutC(Char, CI->getOperand(3), B, TD);
1074 Value *Char = B.CreateLoad(CastToCStr(CI->getOperand(1), B), "char");
1075 EmitFPutC(Char, CI->getOperand(4), B, TD);
10761076 return ConstantInt::get(CI->getType(), 1);
10771077 }
10781078
10961096 return 0;
10971097
10981098 // fputs(s,F) --> fwrite(s,1,strlen(s),F)
1099 uint64_t Len = GetStringLength(CI->getOperand(0));
1099 uint64_t Len = GetStringLength(CI->getOperand(1));
11001100 if (!Len) return 0;
1101 EmitFWrite(CI->getOperand(0),
1101 EmitFWrite(CI->getOperand(1),
11021102 ConstantInt::get(TD->getIntPtrType(*Context), Len-1),
1103 CI->getOperand(1), B, TD);
1103 CI->getOperand(2), B, TD);
11041104 return CI; // Known to have no uses (see above).
11051105 }
11061106 };
11191119
11201120 // All the optimizations depend on the format string.
11211121 std::string FormatStr;
1122 if (!GetConstantStringInfo(CI->getOperand(1), FormatStr))
1122 if (!GetConstantStringInfo(CI->getOperand(2), FormatStr))
11231123 return 0;
11241124
11251125 // fprintf(F, "foo") --> fwrite("foo", 3, 1, F)
11311131 // These optimizations require TargetData.
11321132 if (!TD) return 0;
11331133
1134 EmitFWrite(CI->getOperand(1),
1134 EmitFWrite(CI->getOperand(2),
11351135 ConstantInt::get(TD->getIntPtrType(*Context),
11361136 FormatStr.size()),
1137 CI->getOperand(0), B, TD);
1137 CI->getOperand(1), B, TD);
11381138 return ConstantInt::get(CI->getType(), FormatStr.size());
11391139 }
11401140
11451145
11461146 // Decode the second character of the format string.
11471147 if (FormatStr[1] == 'c') {
1148 // fprintf(F, "%c", chr) --> fputc(chr, F)
1149 if (!CI->getOperand(2)->getType()->isIntegerTy()) return 0;
1150 EmitFPutC(CI->getOperand(2), CI->getOperand(0), B, TD);
1148 // fprintf(F, "%c", chr) --> *(i8*)dst = chr
1149 if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
1150 EmitFPutC(CI->getOperand(3), CI->getOperand(1), B, TD);
11511151 return ConstantInt::get(CI->getType(), 1);
11521152 }
11531153
11541154 if (FormatStr[1] == 's') {
1155 // fprintf(F, "%s", str) --> fputs(str, F)
1156 if (!CI->getOperand(2)->getType()->isPointerTy() || !CI->use_empty())
1155 // fprintf(F, "%s", str) -> fputs(str, F)
1156 if (!CI->getOperand(3)->getType()->isPointerTy() || !CI->use_empty())
11571157 return 0;
1158 EmitFPutS(CI->getOperand(2), CI->getOperand(0), B, TD);
1158 EmitFPutS(CI->getOperand(3), CI->getOperand(1), B, TD);
11591159 return CI;
11601160 }
11611161 return 0;
251251 // If we are passing this argument into call as the corresponding
252252 // argument operand, then the argument is dynamically constant.
253253 // Otherwise, we cannot transform this function safely.
254 if (CI->getOperand(ArgNo) == Arg)
254 if (CI->getOperand(ArgNo+1) == Arg)
255255 return true;
256256 }
257257
453453 // required PHI nodes, add entries into the PHI node for the actual
454454 // parameters passed into the tail-recursive call.
455455 for (unsigned i = 0, e = CI->getNumOperands()-1; i != e; ++i)
456 ArgumentPHIs[i]->addIncoming(CI->getOperand(i), BB);
456 ArgumentPHIs[i]->addIncoming(CI->getOperand(i+1), BB);
457457
458458 // If we are introducing an accumulator variable to eliminate the recursion,
459459 // do so now. Note that we _know_ that no subsequent tail recursion
381381 std::vector
382382 Constraints = IA->ParseConstraints();
383383
384 unsigned ArgNo = 0; // ArgNo - The operand of the CallInst.
384 unsigned ArgNo = 1; // ArgNo - The operand of the CallInst.
385385 for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
386386 TargetLowering::AsmOperandInfo OpInfo(Constraints[i]);
387387
449449
450450 if (CallInst *CI = dyn_cast(U)) {
451451 InlineAsm *IA = dyn_cast(CI->getCalledValue());
452 if (!IA) return true;
452 if (IA == 0) return true;
453453
454454 // If this is a memory operand, we're cool, otherwise bail out.
455455 if (!IsOperandAMemoryOperand(CI, IA, I, TLI))
394394 FT->getParamType(2) != TD->getIntPtrType(Context) ||
395395 FT->getParamType(3) != TD->getIntPtrType(Context))
396396 return false;
397
398 if (isFoldable(3, 2, false)) {
399 EmitMemCpy(CI->getOperand(0), CI->getOperand(1), CI->getOperand(2),
397
398 if (isFoldable(4, 3, false)) {
399 EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
400400 1, false, B, TD);
401 replaceCall(CI->getOperand(0));
401 replaceCall(CI->getOperand(1));
402402 return true;
403403 }
404404 return false;
417417 FT->getParamType(2) != TD->getIntPtrType(Context) ||
418418 FT->getParamType(3) != TD->getIntPtrType(Context))
419419 return false;
420
421 if (isFoldable(3, 2, false)) {
422 EmitMemMove(CI->getOperand(0), CI->getOperand(1), CI->getOperand(2),
420
421 if (isFoldable(4, 3, false)) {
422 EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
423423 1, false, B, TD);
424 replaceCall(CI->getOperand(0));
424 replaceCall(CI->getOperand(1));
425425 return true;
426426 }
427427 return false;
435435 FT->getParamType(2) != TD->getIntPtrType(Context) ||
436436 FT->getParamType(3) != TD->getIntPtrType(Context))
437437 return false;
438
439 if (isFoldable(3, 2, false)) {
440 Value *Val = B.CreateIntCast(CI->getOperand(1), B.getInt8Ty(),
438
439 if (isFoldable(4, 3, false)) {
440 Value *Val = B.CreateIntCast(CI->getOperand(2), B.getInt8Ty(),
441441 false);
442 EmitMemSet(CI->getOperand(0), Val, CI->getOperand(2), false, B, TD);
443 replaceCall(CI->getOperand(0));
442 EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), false, B, TD);
443 replaceCall(CI->getOperand(1));
444444 return true;
445445 }
446446 return false;
461461 // st[rp]cpy_chk call which may fail at runtime if the size is too long.
462462 // TODO: It might be nice to get a maximum length out of the possible
463463 // string lengths for varying.
464 if (isFoldable(2, 1, true)) {
465 Value *Ret = EmitStrCpy(CI->getOperand(0), CI->getOperand(1), B, TD,
464 if (isFoldable(3, 2, true)) {
465 Value *Ret = EmitStrCpy(CI->getOperand(1), CI->getOperand(2), B, TD,
466466 Name.substr(2, 6));
467467 replaceCall(Ret);
468468 return true;
478478 !FT->getParamType(2)->isIntegerTy() ||
479479 FT->getParamType(3) != TD->getIntPtrType(Context))
480480 return false;
481
482 if (isFoldable(3, 2, false)) {
483 Value *Ret = EmitStrNCpy(CI->getOperand(0), CI->getOperand(1),
484 CI->getOperand(2), B, TD, Name.substr(2, 7));
481
482 if (isFoldable(4, 3, false)) {
483 Value *Ret = EmitStrNCpy(CI->getOperand(1), CI->getOperand(2),
484 CI->getOperand(3), B, TD, Name.substr(2, 7));
485485 replaceCall(Ret);
486486 return true;
487487 }
6565
6666 // Next, create the new invoke instruction, inserting it at the end
6767 // of the old basic block.
68 SmallVector InvokeArgs(CI->op_begin(), CI->op_end() - 1);
68 SmallVector InvokeArgs(CI->op_begin()+1, CI->op_end());
6969 InvokeInst *II =
7070 InvokeInst::Create(CI->getCalledValue(), Split, InvokeDest,
7171 InvokeArgs.begin(), InvokeArgs.end(),
18461846 default: Out << " cc" << CI->getCallingConv(); break;
18471847 }
18481848
1849 Operand = CI->getCalledValue();
18501849 const PointerType *PTy = cast(Operand->getType());
18511850 const FunctionType *FTy = cast(PTy->getElementType());
18521851 const Type *RetTy = FTy->getReturnType();
18701869 writeOperand(Operand, true);
18711870 }
18721871 Out << '(';
1873 for (unsigned op = 0, Eop = CI->getNumOperands() - 1; op < Eop; ++op) {
1874 if (op > 0)
1872 for (unsigned op = 1, Eop = I.getNumOperands(); op < Eop; ++op) {
1873 if (op > 1)
18751874 Out << ", ";
1876 writeParamOperand(CI->getOperand(op), PAL.getParamAttributes(op + 1));
1875 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op));
18771876 }
18781877 Out << ')';
18791878 if (PAL.getFnAttributes() != Attribute::None)
19171916 writeOperand(Operand, true);
19181917 }
19191918 Out << '(';
1920 for (unsigned op = 0, Eop = II->getNumOperands() - 3; op < Eop; ++op) {
1919 for (unsigned op = 0, Eop = I.getNumOperands() - 3; op < Eop; ++op) {
19211920 if (op)
19221921 Out << ", ";
1923 writeParamOperand(II->getOperand(op), PAL.getParamAttributes(op + 1));
1922 writeParamOperand(I.getOperand(op), PAL.getParamAttributes(op + 1));
19241923 }
19251924
19261925 Out << ')';
337337 if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD ||
338338 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
339339 std::vector Idxs;
340 Value *Op0 = CI->getOperand(0);
340 Value *Op0 = CI->getOperand(1);
341341 ShuffleVectorInst *SI = NULL;
342342 if (isLoadH || isLoadL) {
343343 Value *Op1 = UndefValue::get(Op0->getType());
344 Value *Addr = new BitCastInst(CI->getOperand(1),
344 Value *Addr = new BitCastInst(CI->getOperand(2),
345345 Type::getDoublePtrTy(C),
346346 "upgraded.", CI);
347347 Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI);
374374 SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI);
375375 } else if (isMovSD ||
376376 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
377 Value *Op1 = CI->getOperand(1);
377 Value *Op1 = CI->getOperand(2);
378378 if (isMovSD) {
379379 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
380380 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
388388 Value *Mask = ConstantVector::get(Idxs);
389389 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
390390 } else if (isShufPD) {
391 Value *Op1 = CI->getOperand(1);
392 unsigned MaskVal = cast(CI->getOperand(2))->getZExtValue();
391 Value *Op1 = CI->getOperand(2);
392 unsigned MaskVal = cast(CI->getOperand(3))->getZExtValue();
393393 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), MaskVal & 1));
394394 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C),
395395 ((MaskVal >> 1) & 1)+2));
409409 CI->eraseFromParent();
410410